I have many files in different paths with various file extensions.
My input.txt
content as follows,
/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
.......................
...........................
..............................
I need to filter and move the specified extension files from all occurrences of input.txt
file to output.txt
file.
For this, i have below script.
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.')
Expected output.txt
should be like below.
/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
Above script doesn't work, it fails with below errors
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'
What is causing this issue?
Any help would be appreciated to filter & move the files to output.txt
file.
Note: Moved files shouldn't be exist in input.txt
file.