1

I was making a script that renames files in a given directory into "CoronaVirus" and when testing it I noticed that it only does it in the given directory and not in it's subfolders. The code looks something like this(note: this one works):

import os

PRF = input("Input Directory: ")

def main():

for file in PRF:
    for filename in os.listdir(PRF):
          i = 0
          dst = "CoronaVirus" + str(i)
          src = PRF + filename
          dst = PRF + dst
          os.rename(src , dst)
          i += 1
if __name__ == "__main__"":
    main()

After I saw that it only goes through the given directory I tried adding a new statement(forgive me if that's not what it is called) called subdir thinking that it would make it look throuh folders. That made the code look like this:

import os

PRF = input("Input Directory: ")

def main():

for subdir , file in PRF:
    for filename in os.listdir(PRF):
           i = 0
           dst = "CoronaVirus" + str(i)
           src = PRF + filename
           dst = PRF + dst
           os.rename(src , dst)
           i += 1
if __name__ == "__main__"":
    main()

But this doesn't seem to work as it gives this error:

Traceback (most recent call last):
File "C:/Users/Acer/Desktop/Python/Pyinstaller/OSM.py", line 17, in <module>
main()
File "C:/Users/Acer/Desktop/Python/Pyinstaller/OSM.py", line 7, in main
for  subdir , file in PRF:
ValueError: not enough values to unpack (expected 2, got 1)

Can somebody explain why this is happening? I'm a beginner and would like to avoid this issue in the future. Thanks!

Ck 3000
  • 31
  • 4
  • 1
    Please provide full traceback. Also please indent your code correctly - the code you pasted here, indented as it is, should produce a syntax error. – Błotosmętek Mar 04 '20 at 20:23
  • 1
    what do you expect this line `for subdir , file in PRF:` to do? – Tomerikoo Mar 04 '20 at 20:23
  • Does this answer your question? [Python script recursively rename all files in folder and subfolders](https://stackoverflow.com/questions/41861238/python-script-recursively-rename-all-files-in-folder-and-subfolders) – Tomerikoo Mar 04 '20 at 20:28
  • You should just remove `for subdir , file in PRF:` – blacksite Mar 04 '20 at 20:37

2 Answers2

0

If you want it to go through the current directory and all subdirectories I would use os.walk

I'm not sure exactly what order this will go through the files but at least it will go through them all.

I also had to remove the i=0 from the loop and add something that ensures the extension is kept

If there are zip files, you probably should extract these into the folder first as this won't look in them and would instead name the zip folder 'CoronaVirusN.zip'

I put in an if to exclude zip files

PRF = 'C:\\Users\\ALi\\OneDrive\\StackOverflow\\Test'

i = 0
for subdir, dirs, files in os.walk(PRF):
    for file in files:
        extension = file.split('.')[1]
        if extension != 'zip':
            dst = "CoronaVirus" + str(i) + '.' + extension
            src = subdir + '\\' + file
            dst = subdir + '\\' + dst
            os.rename(src , dst)
            i += 1
Alistair
  • 589
  • 3
  • 11
  • This works but stoppes when it encounters a file it can't rename for some reason it gives a windows error. – Ck 3000 Mar 04 '20 at 20:52
  • This is the error: Traceback (most recent call last): File "C:/Users/Acer/Desktop/Python/Pyinstaller/OSM.py", line 18, in main() File "C:/Users/Acer/Desktop/Python/Pyinstaller/OSM.py", line 13, in main os.rename(src, dst) FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\Acer\\Desktop\\Python\\Pyinstaller\\dist\\LoVYa - Copybase_library.zip' -> 'C:\\Users\\Acer\\Desktop\\Python\\Pyinstaller\\dist\\LoVYa - CopyCoronaVirus0.zip' – Ck 3000 Mar 04 '20 at 20:55
  • I've added a note to my answer – Alistair Mar 04 '20 at 20:58
  • I'm going to reply some more tomorrow because in my timezone it's pretty late and I have to go to sleep otherwise I'll be a zombie tomorrow :) – Ck 3000 Mar 04 '20 at 20:59
  • Oh and it stil gives a win error except it renames all the files in the given directory but not the folders. It might be because the file it's giving the new error for has a .h extension. – Ck 3000 Mar 04 '20 at 21:00
  • No worries, my new answer will be waiting – Alistair Mar 04 '20 at 21:13
0

The input, PRF, is not a tuple containing 2 values, which is expected by the for loop:

for subdir , file in PRF:

Further, os.listdir(PRF) requires a string as an argument, so PRF cannot be both a string and a tuple.

I believe what you want is this:

import os

PRF = input("Input Directory: ") # this should be a string that represents a directory

def main():
    # get a list of files within that directory
    files = [f for f in os.listdir(PRF) if os.path.isfile(f)]
    # loop through files
    for filename in files:
        i = 0
        dst = "CoronaVirus" + str(i)
        src = PRF + filename
        dst = PRF + dst
        os.rename(src , dst)
        i += 1
if __name__ == "__main__""
   main()

See here for how the files variable is set: List files in directory

jurms22
  • 101
  • 1
  • 3
  • This is more or less similar to the first code OP has. I believe the problem was how to recursively rename all files inside the subdirectories of `PRF` – Tomerikoo Mar 04 '20 at 20:38
  • This seems like the good answer but for some reason when i paste it in it does nohing. Renames nothing , changes nothing. I even tried multiple directories but it didn't work. – Ck 3000 Mar 04 '20 at 20:47
  • It looks like @Alistair provided a better answer that will include all subdirectories. This answer above will not get files in subdirectories, but it does fix the error message you were getting. – jurms22 Mar 04 '20 at 21:25