-1

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.

user4948798
  • 1,924
  • 4
  • 43
  • 89
  • You're trying to move the files but they can't be found. Sure they exist and Python script can move them? – 0stone0 Jun 16 '23 at 10:02
  • Since i'm trying to move the matched extension files from my `input.txt` to `output.txt` file. So in my `input.txt` i have `/path/to/dir1/readme.html` file. – user4948798 Jun 16 '23 at 10:04
  • The error message seems pretty explicit! In addition, the line causing an error isn't relevant to the goal you explained; furthermore, your expected output doesn't match your specifications (why shouldn't `/path/to/dir1/a.html` be in output,txt, for example?) – Swifty Jun 16 '23 at 10:05
  • Yea but your moving them on your file system to have '. processed' behind the filename, but that fails. So the script can't find them. – 0stone0 Jun 16 '23 at 10:05
  • ah it just need to search in `input.txt` file if matched extension files found. it just need to moved to `output.txt` file. – user4948798 Jun 16 '23 at 10:07
  • Do you want to move (or copy) the file_path line, or the file itself? This is quite unclear. – Swifty Jun 16 '23 at 10:08
  • So you don't actually want to rename the file on your system? – 0stone0 Jun 16 '23 at 10:08
  • I want move the matched extension files from my `input.txt` file to `output.txt` file. – user4948798 Jun 16 '23 at 10:09
  • right. i dont want to rename them. – user4948798 Jun 16 '23 at 10:10
  • Then the `shutil...` line shouldn't exist. – Swifty Jun 16 '23 at 10:13
  • You're missing `/path/to/dir1/a.html` in your expected output since `.html` is in your list. – 0stone0 Jun 16 '23 at 14:50

2 Answers2

1

shutil.move moves/renames the file on your filesystem.

That fails, probably because the file does not exist, or the python script does not have permission to move it.


If you just want to append the name to output.txt, remove the shutil line.

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.')

Now if I run the script, the output.txt contains:

/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

That said, consider using the following, using os.path.splitext to get the file extension, then you can just do an if ext in file_extensions, so you don't need the any and loop:

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.')
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • it moved all the files. didn't consider my specified file extensions. – user4948798 Jun 16 '23 at 10:19
  • Oeps, had a typo in the second code block. Fixed now. – 0stone0 Jun 16 '23 at 10:21
  • 1
    @0stone0: it seems you forgot the last file (.json) in your output.txt listing; that said, both codes yield the correct output :) – Swifty Jun 16 '23 at 10:33
  • Did it work for you @user4948798? – 0stone0 Jun 16 '23 at 13:26
  • @0stone0 - I have many files in my input.txt file, but your script copying the specified extension files to output.txt instead of moving them. – user4948798 Jun 16 '23 at 14:11
  • [You said](https://stackoverflow.com/questions/76489042/search-and-move-the-specified-extension-to-other-file/76489146?noredirect=1#comment134866564_76489042) that you didn't want to move the files. – 0stone0 Jun 16 '23 at 14:12
  • No, i want filter based on provided file extension and move those files to `output.txt` file from my `input.txt` file. – user4948798 Jun 16 '23 at 14:19
  • Oke, then I still don't understand whats going wrong. The output.txt contains only those files from input.txt which extension matches your list. Please let me know what (which line) doesn't work for you. – 0stone0 Jun 16 '23 at 14:24
  • Actually i have 12 Lakhs files in my `input.txt` file from that your script filtered and copying them to `output.txt` file instead of moving those files. Please help me to move them. – user4948798 Jun 16 '23 at 14:34
  • Dude, please keep it clear. Your last message is contradicting. Currently, the filesnames are saved in `output.txt`, **and are NOT moved or altered in any way**. What's missing?? – 0stone0 Jun 16 '23 at 14:39
  • Files are `Not moved` – user4948798 Jun 16 '23 at 14:41
  • What's that support to mean? Do they need to be moved? As you said they shouldn't. I give up, good luck m8 – 0stone0 Jun 16 '23 at 14:42
  • yes right `['.html', '.c', '.cpp', '.h', '.py', '.txt', '.js', '.json', '.csv']` extension files need to moved to `output.txt` file. – user4948798 Jun 16 '23 at 14:44
  • Yes thats what happening, as already stated many times. – 0stone0 Jun 16 '23 at 14:50
  • Though this is FAR from clear, I think (or rather guess) that the OP wants the entries "moved" to output.txt to be deleted from input.txt ; could you (@user4948798) confirm this is the case, and if so edit your question to make it clear? – Swifty Jun 16 '23 at 16:05
  • Yes you are right, I want the entries to be moved to output.txt, after moving it shouldn't exist in input.txt file. – user4948798 Jun 16 '23 at 16:19
  • Ahhh, now it makes sense. That's not a lot of changes since you already know the line. I'm away from my PC now but I'll try to update my answer later this evening. – 0stone0 Jun 16 '23 at 17:00
-1

As was said earlier, the error comes from the file you tried to move not existing.

But since you don't need to actually move the files, only to filter your input file, no need for shutil.move. Here's a working solution, updated to rewrite input.txt with only the unmoved lines:

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
Swifty
  • 2,630
  • 2
  • 3
  • 21