0

I know similar questions have been asked a few times on this site, but the solutions provided there did not work for me.

I need to rename files with titles such as

a.jpg
'b.jpg'
c.jpg
"d.jpg"

to

a.jpg
b.jpg
c.jpg
d.jpg

Some of these titles have quotation marks inside the title as well, but it doesn't matter whether they get removed or not.

I have tried

import os
import re

fnames = os.listdir('.')
for fname in fnames:
   os.rename(fname, re.sub("\'", '', fname))

and

import os
for file in os.listdir("."):
  os.rename(file, file.replace("\'", "")) 

to then do the same for the " quotation mark as well, but the titles remained unchanged. I think it might be due to listdir returning the filenames with ' quotation marks around them, but I am not sure.

Edit: I am working on a Ubuntu 18.04.

5
  • what is your operating system? Commented Mar 15, 2019 at 15:45
  • it works on windows, what is the current directory? can you print the files you're trying ot rename? Commented Mar 15, 2019 at 15:49
  • I am working on an Ubuntu server. There's neither rename installed nor do I have the rights to install it, so I opted for Python. I have just tried to print the new file names and they seem to be corrected properly, but the filenames remain unchanged. Commented Mar 15, 2019 at 16:00
  • Works on Ubuntu 18.04 desktop with Python 3.7. What do you mean corrected properly but remain unchanged? Commented Mar 15, 2019 at 16:04
  • I mean that if I print the changed filenames then the quotation mark is removed properly, but if I check the directory they are still unchanged. Commented Mar 15, 2019 at 16:15

2 Answers 2

1

On windows, a filename with double quotes in it isn't a valid filename. However, a filename with single quotes is valid.

A string with double quotes in it in python would look like:

'"I\'m a string with a double quote on each side"'

A string with single quotes in it in python would look like:

"'I\'m a string with a single quote on each side'"

Because you can't have a double-quote filename on windows, you can't os.rename('"example.txt"', "example.txt"). Because it can't exist to even be renamed.

You can put this script on your desktop and watch the filenames change as it executes:

import os

open("'ex'am'ple.t'xt'",'w')
input("Press enter to rename.")
#example with single quotes all over the filename
os.rename("'ex'am'ple.t'xt'", "example.txt")

open("'example.txt'",'w')
input("Press enter to rename.")
#example with single quotes on each side of filename
os.rename("'example2.txt'", "example2.txt")
Sign up to request clarification or add additional context in comments.

2 Comments

I am working on a ubuntu 18.04, should have clarified that in the post, sorry.
@Shikah If filenames with double quotes are valid in Ubuntu then os.rename('"example.txt"', "example.txt") should work.
1

Here is my attempt using a for-loop, like you do and list comprehension used on the string, which is also an iterable.

import os

files = os.listdir(os.getcwd())
for file in files:
    new_name = ''.join([char for char in file if not char == '\''])
    print(new_name)
    os.rename(file, new_name)

Edit the forbidden_chars list with the characters, that you do not want in the future filename. Remember that this will also change folder names afaik, so you may want to check at the start of the for-loop if os.isfile(file): before changing the name. I actually do not understand how you would have filenames, that include the extension inside of quotation marks, but this should work either way. I highly recommend being careful if you want to remove dots. I also recommend peeking at the documentation for the os module, before using its functions as they can do things you may not be aware of. For example: renaming to an existing filename within the directory will just silently replace the file.

4 Comments

I am getting "TypeError: rename: dst should be string, bytes or os.PathLike, not list" with this solution.
I think the file in files is no string, so the list comprehension doesn't create a new string. Does adding file = str(file) to the top of the for-loop solve your issue?
Use str.join on the list
That works! I should have tested my code properly. That is my fault. I updated the code sample. I removed the list of forbidden characters, but i am sure you can do that yourself by now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.