【问题标题】:Delete text files that don't have specific words in them from directory从目录中删除没有特定单词的文本文件
【发布时间】:2018-10-22 19:42:55
【问题描述】:

我有一个目录,其中包含 ~2200 个文本文件。我需要删除任何不包含我定义的特定单词的文本文件。有人可以看看这段代码并就如何让它工作提出建议吗?现在,当我运行它时,它说找不到目录“C”。

另外,我想确保该目录中的每个文件都运行它。我需要包含下一个功能吗?

import os

path = r'C:\Users\user\Desktop\AFL codes to test'
words = ['buy', 'sell']

for root, dirs, files in os.walk(path):
    for file in path:
        if not any(words in file for words in words):
            os.remove(file)

另外,这里是完整的回溯:

runfile('C:/Users/user/.spyder-py3/DELETE FILES THAT DONT CONTAIN CERTAIN WORDS.py', wdir='C:/Users/user/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-23-dbc80e182b2b>", line 1, in <module>
    runfile('C:/Users/user/.spyder-py3/DELETE FILES THAT DONT CONTAIN CERTAIN WORDS.py', wdir='C:/Users/user/.spyder-py3')

  File "C:\Users\user\Anaconda31\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\user\Anaconda31\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/user/.spyder-py3/DELETE FILES THAT DONT CONTAIN CERTAIN WORDS.py", line 9, in <module>
    os.remove(file)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C'

This is the error after trying shutil.rmtree

runfile('C:/Users/user/.spyder-py3/DELETE FILES THAT DONT CONTAIN CERTAIN WORDS.py', wdir='C:/Users/user/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-16-dbc80e182b2b>", line 1, in <module>
    runfile('C:/Users/user/.spyder-py3/DELETE FILES THAT DONT CONTAIN CERTAIN WORDS.py', wdir='C:/Users/user/.spyder-py3')

  File "C:\Users\user\Anaconda31\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\user\Anaconda31\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/user/.spyder-py3/DELETE FILES THAT DONT CONTAIN CERTAIN WORDS.py", line 12, in <module>
    shutil.rmtree(full_path)

  File "C:\Users\user\Anaconda31\lib\shutil.py", line 494, in rmtree
    return _rmtree_unsafe(path, onerror)

  File "C:\Users\user\Anaconda31\lib\shutil.py", line 376, in _rmtree_unsafe
    onerror(os.listdir, path, sys.exc_info())

  File "C:\Users\user\Anaconda31\lib\shutil.py", line 374, in _rmtree_unsafe
    names = os.listdir(path)

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:/Users/user/Desktop/AFL codes to test/newfile1.txt'

【问题讨论】:

  • 您要删除那些inside文档或文档name中没有文本的那些?
  • 对于您的路径问题 - 检查此答案:stackoverflow.com/a/2953843/366965
  • 我想删除所有没有指定文本的 .txt 文件,不管文件名是什么。
  • @JoeNiland,这不是问题,可以,但在这种情况下不是
  • 你能发一个完整的tracebak请求吗

标签: python os.walk


【解决方案1】:

你应该用正斜杠替换反斜杠。

path = r'C:\Users\user\Desktop\AFL codes to test'

应该是

path = 'C:/Users/user/Desktop/AFL codes to test'

编辑:这里是应该让你去的完整代码:

import os

path = 'C:/Users/user/Desktop/AFL codes to test'
words = ['buy', 'sell']

files = os.listdir(path)
for each_file in files:
    full_path = "%s/%s" % (path, each_file)
    each_file_content = open(full_path, 'r', encoding="utf-8").read()
    if not any(word in each_file_content for word in words):
       os.unlink(full_path)

【讨论】:

  • @Netwave 他得到错误“现在,当我运行它时它说它找不到目录'C'”。我相信他的字符串中的反斜杠会导致这种情况。你有什么意见?
  • 他正在遍历path字符串,所以其中的第一个元素是'C',所以当然除非在他的cwd中有一个目录或文件,否则它无法找到
  • 没错。我编辑了我的代码。如果可行,您可以尝试报告吗?
  • 我不知道os.walk(path) 是如何工作的,所以我使用了os.listdir。我希望能为你解决这个问题。
  • 工作!我只需要将编码添加为 utf-8。谢谢你不放弃:)
猜你喜欢
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-04
  • 1970-01-01
相关资源
最近更新 更多