Jump to content

Rename each file in a folder using an external file

Aspect11

So I'm trying to create a where there is a folder of files each of which need to be renamed and there is an text document with all the filenames that need to be used inside. I basically want it to loop through and rename each file in the folder to the corresponding one in the text document. My current code:

import os

names = []
directory = "1/"

with open("names.txt") as file:
    for line in file:
        names.append(line)

print(names)

for filename in os.listdir(directory):
    i = 0
    print(filename)
    os.rename(directory + filename, directory + names[i] + '.pdf')
    i += 1

But I keep getting the error:

Quote

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '1/#NoFilter.pdf' -> "1/& all my problems (it's so stupid, they're not even problems!)\n.pdf"

Anyone know what's wrong?

 

Thanks!

Specs:

Motherboard: Gigabyte Z97X Gaming 3

CPU: Intel Core I7 4790K

GPU: Gigabyte G1 Gaming GTX 970

RAM: HyperX Fury 16GB

HDD: Seagate ST3000DM001 3TB

SSD: KINGSTON SV300S37A480G 450GB

Cooler: Corsair H100i GTX

Case: NZXT H440 Red

Monitor: DELL U2412M

Keyboard: Gigabyte Force K7

Mouse: Corsair Sabre RGB

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, boggy77 said:

you need to specify the directory name with a full path

But it still does read the filenames from the folder. I only can't seem to increment through each value in the array.

Specs:

Motherboard: Gigabyte Z97X Gaming 3

CPU: Intel Core I7 4790K

GPU: Gigabyte G1 Gaming GTX 970

RAM: HyperX Fury 16GB

HDD: Seagate ST3000DM001 3TB

SSD: KINGSTON SV300S37A480G 450GB

Cooler: Corsair H100i GTX

Case: NZXT H440 Red

Monitor: DELL U2412M

Keyboard: Gigabyte Force K7

Mouse: Corsair Sabre RGB

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, boggy77 said:

so which line throws the error?

It's line 15, it doesn't increment through the array

Specs:

Motherboard: Gigabyte Z97X Gaming 3

CPU: Intel Core I7 4790K

GPU: Gigabyte G1 Gaming GTX 970

RAM: HyperX Fury 16GB

HDD: Seagate ST3000DM001 3TB

SSD: KINGSTON SV300S37A480G 450GB

Cooler: Corsair H100i GTX

Case: NZXT H440 Red

Monitor: DELL U2412M

Keyboard: Gigabyte Force K7

Mouse: Corsair Sabre RGB

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Aspect11 said:

It's line 15, it doesn't increment through the array

you know directory are backslash [ \ ] and not front slash [ / ]

Link to comment
Share on other sites

Link to post
Share on other sites

The problem you are having is because each line string includes the newline ('\n') character at the end of the line. Windows does not support '\n' in filenames, hence the error you are getting.

 

You want to change

        names.append(line)

to

        names.append(line.replace('\n', ''))

which gets rid of the problem character.

The Eight Fallacies of Distributed Computing

Essentially everyone, when they first build a distributed application, makes the following eight assumptions. All prove to be false in the long run and all cause big trouble and painful learning experiences.

  1. The network is reliable
  2. Latency is zero
  3. Bandwidth is infinite
  4. The network is secure
  5. Topology doesn’t change
  6. There is one administrator
  7. Transport cost is zero
  8. The network is homogeneous

        — Peter Deutsch

Link to comment
Share on other sites

Link to post
Share on other sites

you need to move the 

i = 0

to above the loop as you're resetting it to 0 every loop

i = 0
for filename in os.listdir(directory):
    print(filename)
    os.rename(directory + filename, directory + names[i] + '.pdf')
    i += 1

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

58 minutes ago, vorticalbox said:

you need to move the 


i = 0

to above the loop as you're resetting it to 0 every loop


i = 0
for filename in os.listdir(directory):
    print(filename)
    os.rename(directory + filename, directory + names[i] + '.pdf')
    i += 1

 

I've done that (kinda stupid me) and I've modified it like:

import os

names = []
directory = "1/"

with open("names.txt") as file:
    for line in file:
        names.append(line.replace('\n', ''))

i = 0

for filename in os.listdir(directory):
    print(filename)
    try:
        os.rename(directory + filename, directory + names[i] + '.pdf')
    except FileExistsError:
        continue
    i += 1

with open("names.txt") as file:
    for line in file:
        print(line)

But the second 'with open' shows that the files have been renamed bu in the file explorer, they haven't

Specs:

Motherboard: Gigabyte Z97X Gaming 3

CPU: Intel Core I7 4790K

GPU: Gigabyte G1 Gaming GTX 970

RAM: HyperX Fury 16GB

HDD: Seagate ST3000DM001 3TB

SSD: KINGSTON SV300S37A480G 450GB

Cooler: Corsair H100i GTX

Case: NZXT H440 Red

Monitor: DELL U2412M

Keyboard: Gigabyte Force K7

Mouse: Corsair Sabre RGB

 

Link to comment
Share on other sites

Link to post
Share on other sites

Windows works with both  / and \ when building the path, but Python's libraries may or may not reject them ... don't know.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×