这里我尝试创建一个代码,根据掩码删除文件夹中的文件。包含17的所有文件应该被删除文件名格式是?? _ ???? 17 *。*,在哪里? - 任何符号1..n,A..z,_和17 - 都在任何文件中(其他文件也包含18个),其扩展名无关紧要。某个文件的例子AB_DEFG17Something.Anything - Copy(2).txt
import os
import re
dir_name = "/Python/Test_folder" # open the folder and read files
testfolder = os.listdir(dir_name)
def matching(r, s): # condition if there's nothing to match
match = re.search(r, s)
if match:
return match.group()
return "Files don't exist!"
matching(r'^\w\w\[_]\w\w\w\w\[1]\[7]\w+\[.]\w+', testfolder) # matching the file's mask
for item in testfolder.index(matching):
if item.name(matching, s):
os.remove(os.path.join(dir_name, item))
# format of filenames not converted : ??_????17*.*
# convert for python separarately : [\w][\w][_\w][\w][\w][\w]\[1]\[7][\w]+[\.][\w]+
# ? - Any symbol 1..n,A..z \w repeating is *
# * - Any number of symbols 1..n, A..z
# _ and 17 - in any files `
也有一些错误。
文件" D:\ Python \ Test_folder \ Remover v2.py",第14行,在 匹配(r' \ w \ w [_] \ w \ w \ w \ w [1] [7] \ w + [。] \ w +',testfolder)#匹配文件' s面具 文件" D:\ Python \ Test_folder \ Remover v2.py",第9行,匹配 match = re.search(r,s) 文件" c:\ Program Files(x86)\ Wing IDE Personal 6.0 \ bin \ runtime-python2.7 \ Lib \ re.py",第146行,搜索 return _compile(pattern,flags).search(string)
我是初学者,有业余的方法,希望获得PY的经验,并行学习细节。我究竟做错了什么?任何帮助都会有用。 THX
答案 0 :(得分:5)
不要重新发明轮子,而是使用glob()
代替:
import os
from glob import glob
for file in glob('/Python/Test_folder/AB_CDEF17*.*'):
os.remove(file)
答案 1 :(得分:2)
for filename in glob.glob(os.path.join(dirname, "AB_CDEF17*.*")):
try:
# Trying to remove a current file
os.remove(os.path.join(dirname, filename))
except EnvironmentError:
# You don't have permission to do it
pass
pattern = re.compile(r"AB_CDEF17\w+\.\w+")
for filename in os.scandir(dirname):
if pattern.match(filename):
try:
os.remove(os.path.join(dirname, filename))
except EnvironmentError:
pass
答案 2 :(得分:1)
您可以直接在shell中使用以下命令:
cd $PATH; for inode in $(ls -il AB_CDEF17*.* | awk '{print $1}'); do find . -type f -inum $inode -exec rm -i {} \;; done
cd $PATH;
转到相关文件夹$(ls -il AB_CDEF17*.* | awk '{print $1}')
将打印当前目录中文件的所有内容,我正在使用此绕道,因为它看起来文件名中有空格,因此rm
命令无法正常运行。find . -type f -inum $inode -exec rm -i {} \;;
根据他们的内容查找文件,并通过征求您的许可将其删除。如果你确定你做了什么,并且你真的想将它嵌入到一些python代码中:
from subprocess import call
call('cd $PATH; for inode in $(ls -il AB_CDEF17*.* | awk '{print $1}'); do find . -type f -inum $inode -exec rm -f {} \;; done')
注意:将rm -f
文件删除,不会要求您确认
答案 3 :(得分:1)
您可以尝试glob解决方案
例如,这些是文件夹
中的文件~/Test-folder$ ls *.txt -1
AB_DEFG17Sitanything.n.txt
AB_DEFG17SOManything.copy(2).txt
AB_DEFG17SOManything.nis.txt
AB_DEFG17SOManything.n.txt
AB_DEFG18SOManything.n.txt
AB_DEFG28SOManything.n.txt
AB_PIZG17SOManything.piz.txt
AB_PIZG28SOManything.n.txt
AB_PIZG76SOManything.n.txt
我的代码
import glob
r = [f for f in glob.glob("*.txt") if "AB_DEFG" in f or "17" in f]
for f in r:
print (f)
你会得到
AB_DEFG17SOManything.n.txt
AB_DEFG17SOManything.nis.txt
AB_PIZG17SOManything.piz.txt
AB_DEFG17Sitanything.n.txt
AB_DEFG28SOManything.n.txt
AB_DEFG17SOManything.copy(2).txt
AB_DEFG18SOManything.n.txt
我忘了添加删除解决方案
import glob,os
r = [f for f in glob.glob("*.txt") if "AB_DEFG" in f or "17" in f]
for f in r:
os.remove(f)
只剩下两个文件
AB_PIZG28SOManything.n.txt
AB_PIZG76SOManything.n.txt