删除python中文件名中任何位置包含数字的文件
我有一个包含.jpg文件的目录,我需要删除该目录中包含文件名中任何数字的所有文件 我已经使用os.listdir来指定路径。然后我可以使用fnmatch.fnmatch只删除一些特定的东西,比如“3453.jpg”,但我需要它来删除一些东西,比如thomas9.jpg或thom43as.jpg。基本上,任何包含数字的文件名都需要删除 谢谢,删除python中文件名中任何位置包含数字的文件,python,Python,我有一个包含.jpg文件的目录,我需要删除该目录中包含文件名中任何数字的所有文件 我已经使用os.listdir来指定路径。然后我可以使用fnmatch.fnmatch只删除一些特定的东西,比如“3453.jpg”,但我需要它来删除一些东西,比如thomas9.jpg或thom43as.jpg。基本上,任何包含数字的文件名都需要删除 谢谢, Tom使用re模块 import os import re for _file in os.listdir('.'): if re.search
Tom使用
re
模块
import os
import re
for _file in os.listdir('.'):
if re.search('\d.*\.jpg$', _file):
print(_file)
使用
re
模块
import os
import re
for _file in os.listdir('.'):
if re.search('\d.*\.jpg$', _file):
print(_file)
regex
在您的情况下并不是必需的,如果您想要删除任何文件名包含任何数字的文件,那么您可以简单地通过以下方式执行:
import os
my_path = 'path_to_your_directory'
any_digit = '1234567890'
os.chdir(my_path) #Just to be sure you are in your working directory not else where!
for f in os.listdir('.'):
if any(x in f for x in any_digit) and f.endswith('.jpg'):
os.remove(f)
您还可以使用string
模块,然后:
import os
import string
my_path = 'path_to_your_directory'
os.chdir(my_path) #Just to be sure you are in your working directory not else where!
for f in os.listdir('.'):
if any(x in f for x in string.digits) and f.endswith('.jpg'):
os.remove(f)
regex
在您的情况下并不是必需的,如果您想要删除任何文件名包含任何数字的文件,那么您可以简单地通过以下方式执行:
import os
my_path = 'path_to_your_directory'
any_digit = '1234567890'
os.chdir(my_path) #Just to be sure you are in your working directory not else where!
for f in os.listdir('.'):
if any(x in f for x in any_digit) and f.endswith('.jpg'):
os.remove(f)
您还可以使用string
模块,然后:
import os
import string
my_path = 'path_to_your_directory'
os.chdir(my_path) #Just to be sure you are in your working directory not else where!
for f in os.listdir('.'):
if any(x in f for x in string.digits) and f.endswith('.jpg'):
os.remove(f)
使用比使用正则表达式稍微简单:
import glob
import os
def delete_files(path, pattern):
for f in glob.iglob(os.path.join(path, pattern)):
try:
os.remove(f)
except OSError as exc:
print exc
>>> delete_files('/tmp', '*[0-9]*.jpg')
os.remove()
应在try/except
块中调用,以防无法删除文件,例如权限不足、被其他进程使用、文件是目录等
使用regex,如果有许多文件,则值得在循环之外编译模式:
import os
import re
def delete_files(path, pattern):
pattern = re.compile(pattern)
for f in os.listdir(path):
if pattern.search(f):
try:
os.remove(os.path.join(path, f))
except OSError as exc:
print exc
delete_files('/tmp', r'\d.*\.jpg$')
使用比使用正则表达式稍微简单:
import glob
import os
def delete_files(path, pattern):
for f in glob.iglob(os.path.join(path, pattern)):
try:
os.remove(f)
except OSError as exc:
print exc
>>> delete_files('/tmp', '*[0-9]*.jpg')
os.remove()
应在try/except
块中调用,以防无法删除文件,例如权限不足、被其他进程使用、文件是目录等
使用regex,如果有许多文件,则值得在循环之外编译模式:
import os
import re
def delete_files(path, pattern):
pattern = re.compile(pattern)
for f in os.listdir(path):
if pattern.search(f):
try:
os.remove(os.path.join(path, f))
except OSError as exc:
print exc
delete_files('/tmp', r'\d.*\.jpg$')
你能发布你到目前为止尝试过的吗?我建议你使用正则表达式(module
re
)来检查文件名是否包含数字。类似于*[0-9]+.*
的内容您可以发布到目前为止尝试过的内容吗?我建议您使用正则表达式(modulere
)检查文件名是否包含数字。如果使用了诸如r'\d.\.jpg$'
之类的正则表达式模式,则不需要*[0-9]+.*
文件.split()。前导的*
是不必要的,因为您正在使用re.search()
。试试看。\u file.split()
如果使用了诸如r'\d.\.jpg$'
之类的正则表达式模式,则不需要使用该文件。@zetysz:\d
就足以匹配。前导的*
是不必要的,因为您正在使用re.search()
。试试看。@zetysz:不,没错。一个数字后跟零个或多个字符(可能包括其他数字)。这是正常的,因为re.search()
匹配字符串中的任何位置,尽管指定一个更完整的模式(如^.*\d.*\.jpg$
匹配行的开头)没有什么害处。哦,您删除了关于regex模式的无效注释。但您是对的,文件不能总是被删除(除非路径与当前目录相同)。修正了。@zetysz:不,没错。一个数字后跟零个或多个字符(可能包括其他数字)。这是正常的,因为re.search()
匹配字符串中的任何位置,尽管指定一个更完整的模式(如^.*\d.*\.jpg$
匹配行的开头)没有什么害处。哦,您删除了关于regex模式的无效注释。但您是对的,文件不能总是被删除(除非路径与当前目录相同)。固定的。