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?
CodePudding user response:
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
CodePudding user response:
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)