python-3.x 过滤指定的文件扩展名到其他文件

gmxoilav  于 2023-06-25  发布在  Python
关注(0)|答案(2)|浏览(122)

我有许多文件在不同的路径与各种文件扩展名。
我的input.txt内容如下,

/path/to/dir1/readme.html
/path/to/dir1/file.c
/path/to/dir1/file1.c
/path/to/dir1/a.html
/path/to/dir2/abc.java
/path/to/dir1/sample.js
/path/to/dir2/a.bin
/path/to/dir1/as.json
.......................
...........................
..............................

我需要过滤并将指定的扩展文件从所有出现的input.txt文件移动到output.txt文件。
为此,我有下面的脚本。

import shutil

input_file = 'input.txt'
output_file = 'output.txt'

file_extensions = ['.html', '.c', '.cpp', '.h', '.py', '.txt', '.js', '.json', '.csv']

with open(input_file, 'r') as input_file, open(output_file, 'w') as output_file:
    for line in input_file:
        file_path = line.strip()

        if any(file_path.endswith(ext) for ext in file_extensions):
            output_file.write(file_path + '\n')
            shutil.move(file_path, file_path + '.processed')

    print('Matching file moved to output.txt.')

预期的output.txt应该如下所示。

/path/to/dir1/readme.html
/path/to/dir1/file.c
/path/to/dir1/file1.c
/path/to/dir1/sample.js
/path/to/dir1/as.json

上面的脚本不起作用,它失败了,并出现以下错误

Traceback (most recent call last):
  File "/usr/lib/python3.8/shutil.py", line 791, in move
    os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: '/path/to/dir1/readme.html' -> '/path/to/dir1/readme.html.processed'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "split_src_libs.py", line 24, in <module>
    shutil.move(file_path, file_path + '.processed')
  File "/usr/lib/python3.8/shutil.py", line 811, in move
    copy_function(src, real_dst)
  File "/usr/lib/python3.8/shutil.py", line 435, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.8/shutil.py", line 264, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: '/path/to/dir1/readme.html'

是什么导致了这个问题?
任何帮助将不胜感激过滤和移动文件到output.txt文件。
注意:移动的文件不应存在于input.txt文件中。

5kgi1eie

5kgi1eie1#

shutil.move移动/重命名文件系统上的文件。
这会失败,可能是因为文件不存在,或者python脚本没有移动它的权限。
如果只想将名称附加到output.txt,请删除shutil行。

input_file = 'input.txt'
output_file = 'output.txt'

file_extensions = ['.html', '.c', '.cpp', '.h', '.py', '.txt', '.js', '.json', '.csv']

with open(input_file, 'r') as input_file, open(output_file, 'w') as output_file:
    for line in input_file:
        file_path = line.strip()

        if any(file_path.endswith(ext) for ext in file_extensions):
            output_file.write(file_path + '\n')

    print('Matching file moved to output.txt.')

现在,如果我运行脚本,output.txt包含:

/path/to/dir1/readme.html
/path/to/dir1/file.c
/path/to/dir1/file1.c
/path/to/dir1/a.html
/path/to/dir1/sample.js
/path/to/dir1/as.json

也就是说,考虑使用以下方法,使用os.path.splitext获取文件扩展名,然后您可以只执行if ext in file_extensions,因此您不需要any和循环:

import os

input_file = 'input.txt'
output_file = 'output.txt'

file_extensions = ['.html', '.c', '.cpp', '.h', '.py', '.txt', '.js', '.json', '.csv']

with open(input_file, 'r') as input_file, open(output_file, 'w') as output_file:
    for line in input_file:
        filename, file_extension = os.path.splitext(line.strip())

        if file_extension in file_extensions:
            output_file.write(line)

    print('Matching file moved to output.txt.')
4szc88ey

4szc88ey2#

如前所述,错误来自您试图移动的file不存在。
但由于您不需要实际移动文件,只需要过滤输入文件,因此不需要shutil.move。下面是一个可行的解决方案,更新为只使用未移动的行重写input.txt:

input_file = 'input.txt'
output_file = 'output.txt'
file_extensions = ['.html', '.c', '.cpp', '.h', '.py', '.txt', '.js', '.json', '.csv']

unmoved_lines = []

with open(input_file, 'r') as _input_file, open(output_file, 'w') as output_file:
    for line in _input_file:
        if f'.{line.strip().split(".")[-1]}' in file_extensions:
            output_file.write(line)
        else:
            unmoved_lines.append(line)
            
with open(input_file, 'w') as _input_file:
    _input_file.write(''.join(unmoved_lines))

output.txt:

/path/to/dir1/readme.html
/path/to/dir1/file.c
/path/to/dir1/file1.c
/path/to/dir1/a.html
/path/to/dir1/sample.js
/path/to/dir1/as.json

input.txt:

/path/to/dir2/abc.java
/path/to/dir2/a.bin

相关问题