Renaming file right after download and moving it to new folder

1.9k views Asked by At

I have script that downloads a file from a site and is saved into my downloads folder. I want to change the name of that file and move it to new folder. I already have code that generates the new filename, but i'm not sure how to take it into effect. This is what I have:

newfile_name='generated from user input'
os.listdir("C:\Users\qzh14\Downloads")
os.rename("originalfile","newfile_name")
shutil.move("CurrentFolder", "NewFolder")
1

There are 1 answers

5
thleo On BEST ANSWER

I'm not sure if that's your full code, but I would use the rename method of os.

import os
mypath = "mydir/myfolder"
new_name = input("What's the new name? ")
# assuming you only have one file in your dir
old_name = os.listdir(mypath)
#create new folder in current dir
new_path = mypath + "newfolder"
# not sure you created your new folder, so I'm using a method to create one
os.renames(old_name, (new_path + "/" + new_name))

Tell me if I've missed anything. I'm relatively new to Python but I've been doing similar work. Hope this helps :)

Update: Searching for a file If you want to select a file from the available files, you can look at which files are there an manually enter the file you want. Something like this:

in_folder = os.listdir(mypath)
print(in_folder)
old_name = input("Which of these files? ")