Python 删除控制器中包含字符串[]的所有文件
我有一个包含5.txt文件的文件夹:Python 删除控制器中包含字符串[]的所有文件,python,Python,我有一个包含5.txt文件的文件夹: 100240042.txt 102042044.txt 016904962.txt 410940329.txt 430594264.txt 其中一种只含有不同类型的水果(如苹果、香蕉、橘子等)。然而,其他的都含有“鸡肉”。这些必须删除,只剩下水果列表 到目前为止,我已经尝试了4种不同的解决方案 尝试0 import os for filename in os.listdir(r'C:\Users\Steve\AppData\Local\Programs\P
100240042.txt
102042044.txt
016904962.txt
410940329.txt
430594264.txt
其中一种只含有不同类型的水果(如苹果、香蕉、橘子等)。然而,其他的都含有“鸡肉”。这些必须删除,只剩下水果列表
到目前为止,我已经尝试了4种不同的解决方案
尝试0
import os
for filename in os.listdir(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits'):
f = open(filename)
for line in filename:
if 'chicken' in line:
found=true
os.remove(filename)
f.close()
尝试1
import os
for file in os.listdir(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits'):
open(file, 'r')
f.read()
find('chicken')
os.remove()
f.close()
尝试2
import os
for file in os.listdir(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits'):
open(file, 'r')
f.read()
find('chicken')
os.remove()
f.close()
尝试3
import os
for file in os.listdir(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits'):
if 'chicken' in open(file).read():
os.remove(file)
f.close()
我认为这是一个相对简单的问题,但我经常会遇到以下错误:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '100240042.txt'
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
FileNotFoundError:[Errno 2]没有这样的文件或目录:“100240042.txt”
您必须构建文件的完整路径。如果您看到错误消息
FileNotFoundError: [Errno 2] No such file or directory: '100240042.txt'
它正试图打开相对于脚本路径的文件。简而言之,它是在与脚本相同的目录中查找此文件
要获得绝对路径,请执行以下操作
os.path.abspath("myFile.txt")
您必须构造文件的完整路径。如果您看到错误消息
FileNotFoundError: [Errno 2] No such file or directory: '100240042.txt'
它正试图打开相对于脚本路径的文件。简而言之,它是在与脚本相同的目录中查找此文件
要获得绝对路径,请执行以下操作
os.path.abspath("myFile.txt")
我看到了其他问题,但让我谈谈你的问题:
os.remove(filename)
这是在当前目录下执行的。通常是运行程序的目录。但是,如果您尝试在shell上运行命令rmfilename
,您也会看到错误,因为该文件实际上位于另一个目录中。您要做的是:
open(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits', filename))
而且:
os.remove(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits', filename))
因此,您的代码应该如下所示:
DIR = r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits'
for filename in os.listdir(DIR):
found = False
with open(os.path.join(DIR, filename)) as f:
for line in f:
if 'chicken' in line:
found = True
break
if found:
os.remove(os.path.join(DIR, filename))
我看到了其他问题,但让我谈谈你的问题:
os.remove(filename)
这是在当前目录下执行的。通常是运行程序的目录。但是,如果您尝试在shell上运行命令rmfilename
,您也会看到错误,因为该文件实际上位于另一个目录中。您要做的是:
open(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits', filename))
而且:
os.remove(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits', filename))
因此,您的代码应该如下所示:
DIR = r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits'
for filename in os.listdir(DIR):
found = False
with open(os.path.join(DIR, filename)) as f:
for line in f:
if 'chicken' in line:
found = True
break
if found:
os.remove(os.path.join(DIR, filename))
谢谢你的回复!我现在将代码更改为如下
import os for filename in os.listdir(r'C:\Users\Steve\AppData\Local\Programs\Python\Python 37-32\Fruits'):f=open(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python 37-32\Fruits),用于文件名中的行:if'chicken'in line:found=true os.remove(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits',filename))f.close()
它没有给我错误通知,但它也没有删除必要的文件。这就是为什么我说你有更多问题。你至少应该在关闭后将删除移到。感谢回复!我现在将代码更改为如下在os.listdir中导入os for filename(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits'):f=open(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits',filename)用于文件名中的行:如果行中的“chicken”:则为found=true os.remove(os.path.join(r'C:\Users\Steve\AppData\Local\Programs\Python\Python37-32\Fruits',filename))f.close()
它不会给我错误通知,但也不会删除必要的文件。这就是为什么我说你有更多的问题。你至少应该在关闭后将删除移到。