1

I have a script that takes the a number as command-line argument and moves files with this number into the folder with the respective number.

Example

For example, I have 20 folders named Episode 1 through Episode 20. I also have a total of 120 files, 5 files per folder file1-ep 1, file2-ep1 would move into folder Episode 1, and so on, all the way to file1-ep20, file2-ep20 which would move into folder episode 20, etc.

Code

I already have the script to move the files. It just requires me following on command line: move.py 1 or move.py 2, etc.

My current script:

import os
import sys
import shutil

path = "/Users/Macbook/Final Cut Pro/"

destination_path = f"/Users/Macbook/Final Cut Pro/episode {sys.argv[1]}/"

for (root, dirs, file) in os.walk(path):
    for f in file:
        f = f.lower()
        if check_file(sys.argv[1])in f:
            source = path + f
            destination = destination_path + f
            print(f)
            # shutil.move(source, destination)

Basically, I want the program to loop automatically instead of asking for user input. So that I would just need to supply the start and stop numbers.

What I tried

I've tried putting the nested for loops into another for loop.

for i in range (1,21)

    for loop
    for loop

How can I make a loop so that it moves all the respective files into the folders without any user input?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
bull11trc
  • 61
  • 5

2 Answers2

0

If the name of your file is in the following format file1-ep 1. they you try this

start_number = 1
end_number = 20
for i in range(start_number, end_number+1):
    for (root, dirs, file) in os.walk(path):
        for f in file:
            f = f.lower()
            if i == int(re.findall(r'\d+', f)[0]):
                source = path + f
                destination = destination_path + f
                print(f)
                shutil.move(source, destination)
dev740
  • 111
  • 2
  • 6
  • I already tried it, but it wouldn't work because when `i` is 1 it would catch `1` `11` `21`, etc, and when `i` is `2` it would catch `22`, `12`, etc. Basically any file that contains `1` would get moved, which isn't what I want. thats why I called the function `checked_files` which would check for me.. – bull11trc Dec 12 '22 at 08:41
  • I have updated my answer, can you check if it works? – dev740 Dec 12 '22 at 09:11
  • 1
    Like I said, I already tried it, it doesn't work. – bull11trc Dec 12 '22 at 11:56
  • Did you try the updated answer? And can you state the reason why the updated code doesn't work? Do you get any error? – dev740 Dec 13 '22 at 04:17
  • 1
    you're "hard coding" the file name, the filename will not be in the format `file1-ep 1`, the filename could be anything (file1, file 1, ep1, ep.1. file.1. file. 1.), as long as it contains the corresponding folder number. – bull11trc Dec 14 '22 at 07:19
  • Okay, you can use regex to extract number from the file name, I have updated my answer, try it out. – dev740 Dec 14 '22 at 08:14
  • I can tell it won't work without even testing it. First, your destination path is isn't in the loop, second you don't account for duplicate numbers. For example, when `i = 5` it will proceed to match all files with `5` in it. Including, `55` `15`, etc. – bull11trc Dec 16 '22 at 23:41
0

Turns out my idea of an outer loop was correct, only I was using sys.argv[i] instead of i. I also have to move the destination path inside the loop.

for i in range(int(sys.argv[1]), int(sys.argv[2]) + 1):
    destination_path = f"/Users/Macbook/Final Cut Pro/episode {i}/"
    for f in os.listdir(path):
        if os.path.isfile(os.path.join(path, f)):
            f = f.lower()
            if checked_files(i):
               #move files
bull11trc
  • 61
  • 5
  • Please explain (for others having the same issue) how this code uses the range from command line arguments. Also provide code for function `checked_files(i)` to have a [example]. – hc_dev Dec 16 '22 at 20:16
  • How does the loop differ from [already answered](https://stackoverflow.com/a/74767066/5730279)? It is more complete, having the file-name matching and move-statement. – hc_dev Dec 16 '22 at 20:18
  • did you look at my answer? my answers uses cmd line arguments for the start and stop in the loop. dev740 loop is hardcoded, he assigns a start and stop of 1 and 20. – bull11trc Dec 16 '22 at 23:30
  • @dev740 edited his answer already, before it was not complete. I already posted my answer before he edited the current version. "explain how this code uses the range from command line arguments" Are you seriously asking how a basic `for loop` works? I take the arguments from cmd line, convert it to an `int` to tell the loop how many times to loop and when to stop . – bull11trc Dec 16 '22 at 23:35