python 删除“错误:重复的数据库条目'xxx'”https://bbs.archlinux.org/viewtopic.php?id=107621

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 删除“错误:重复的数据库条目'xxx'”https://bbs.archlinux.org/viewtopic.php?id=107621相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python

import os,re, datetime
d = '/var/lib/pacman/local'

packages = os.listdir(d)
packages.sort()

pkgname_search = re.compile('^(.*?)-[0-9]')

old_packages = []

for pkg1 in packages:
	if pkg1 in old_packages:
		continue
		
	#get package name
	pkgname = pkgname_search.findall(pkg1)[0]
	
	#look for other items with the same package name
	for pkg2 in packages:
		if pkg2 == pkg1:
			continue
		if pkg2 in old_packages:
			continue
		if pkgname == pkgname_search.findall(pkg2)[0]:
			# We now have two duplicate packages, we want to delete the old one
			
			old_package = pkg1
			path1 = os.path.join(d,pkg1)
			path2 = os.path.join(d,pkg2)
			if os.stat(path1).st_mtime > os.stat(path2).st_mtime:
				old_package = pkg2
			
			old_packages.append(old_package)
			#print ('duplicate found:\t')
			#print (pkg1)
			#print (pkg2)
			#print ('old:', old_package)
			
			oldpath = os.path.join(d,old_package)
			target = os.path.join('/var/lib/pacman/OLD',old_package)
			cmd = 'mv "%s" "%s"' % (oldpath, target)
			
			#double-check that the oldpath still exists (it may have been removed in a previous pass)
			if os.path.exists(oldpath):
				print(cmd)
				os.system(cmd)

以上是关于python 删除“错误:重复的数据库条目'xxx'”https://bbs.archlinux.org/viewtopic.php?id=107621的主要内容,如果未能解决你的问题,请参考以下文章

如何用python删除一个文件

python删除列后行变多了

python 列表删除元素问题?

python从列表中删除元素

如何删除python2 安装python3

python--pandas删除