-1

I hope everyone is staying safe and sound.

Could you please help me with file name change in certain directory?

I am writing a script for RDA to download documents from this website, and when I download these files from this website, file names do not have pattern, it is completely random. So I want to rename these files into certain pattern.

For example,

1st downloaded file name: filedownload.pdf rename to: 1234567.pdf

2nd downloaded file name: extractpages.pdf rename to: 1234568.pdf

new names are set in parameter so that part is good but as I don't know what downloaded file name will be, I can't change this file name into new name.

So what I had in mind is for each downloaded file, put it in this folder without any files, and whenever any file is located in this folder, it renames the file and put it in different folder.

Below is the code I wrote;

filepath = "originalFilePath/downloadedfile.pdf"

if os.path.isfile(filepath):
    os.rename(r'originalFilePath/downloadedfile.pdf',
              'newFilePath' + str(parameter['search_number']) + '.pdf')

But I would like to change file names no matter what the downloaded file name is.

Please help, thank you very much!!

martineau
  • 119,623
  • 25
  • 170
  • 301
dkcloud9
  • 149
  • 1
  • 1
  • 7
  • You can use [`os.listdir`](https://docs.python.org/3/library/os.html#os.listdir) or [`os.walk`](https://docs.python.org/3/library/os.html#os.walk) to get file name(s) in a given folder. As you mentioned you do not know the original downloaded file name. – SSC Apr 04 '20 at 03:04

3 Answers3

1

I think your idea is quite correct.

First, you will list all file name in a folder before downloading anything, store that list in a variable, let's say current_files. Then you start downloading files from the web, after that, you list all the file name again and store in the second list. Now you will compare 2 lists and know what files are new (just downloaded). Then you just simply loop over these new files and rename it in a pattern that you want. I will add code in a minute

import glob
current_files = glob.glob("/path/Downloads/*.pdf")
# ['path/Downloads/abc.pdf', 'path/Downloads/xyz.pdf']

# your code to download files from the web

new_files = set(glob.glob("/path/Downloads/*.pdf")) - set(current_files)
# ['path/Downloads/just_downloaded1.pdf', 'path/Downloads/just_downloaded2.pdf']

for file in new_files:
    #do the rename code here to your new files
Binh
  • 1,143
  • 6
  • 8
0

You question is not totally clear.

Anyway I think you have an issue with your code, you have to add / after newFilePath.

Minions
  • 5,104
  • 5
  • 50
  • 91
0

Use os.renames(old, new).

os.renames(old, new) Recursive directory or file renaming function. Works like rename(), except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned away using removedirs().

References

https://docs.python.org/3/library/os.html

CypherX
  • 7,019
  • 3
  • 25
  • 37