Caitlin
Caitlin

Reputation: 93

Python: Trying to use "{}.replace()" with .format()

A fairly simple question, but for the life of me I can't figure it out...

I have the following piece of code:

os.rename(FileName, FileName.replace(".png","")+"_"+time_stamp.replace(":","-").replace(" ","_")+"_"+name_string+".png")

Basically renaming an image file.

I want to use the .format() structure, but I can't figure out how to get the .replace() function to work with this.

At the moment my converted code looks like this:

os.rename(FileName, "{}.replace(".png","")_{}.replace(":","-").replace(" ","_")_{}.png".format(FileName,time_stamp,name_string))

At the moment the error being given is "invalid syntax" at the first replace ( a caret at the "" in (".png","")).

Could someone kindly point me in the right direction for resources to help with this?

Is there a better way of doing what I'm trying to?

Thanks

Upvotes: 3

Views: 21700

Answers (4)

acdr
acdr

Reputation: 4716

The basic form of the format you're using is this:

"Hello {}".format("there")

which evaluates to output string

"Hello there"

In its simple form, each {} in your string will get replaced by the strings in format, in order. More detailed info, you can find here: https://pyformat.info/

Upvotes: 4

someone
someone

Reputation: 31

what are you need to do is

os.rename(FileName, "{0}_{1}_{2}".format(FileName.replace(".png",""), 
                                        time_stamp.replace(":","-").replace(" ","_"),
                                        name_string+".png"))

or more readable

newName = "{rmvFileFormat}_{time}_{addFileFormat}".format(rmvFileFormat = FileName.replace(".png",""), 
                                                          time = time_stamp.replace(":","-").replace("","_"),
                                                          addFileFormat = name_string+".png")
os.rename(FileName, newName)

Upvotes: 2

xiº
xiº

Reputation: 4687

You shouldn't mix python string operation replace() with arguments for format().

os.rename(FileName, '{0}_{1}_{2}.png'.format(
    FileName.replace(".png", ""),
    time_stamp.replace(":", "-"),
    name_string.replace(" ", "_")
))

Upvotes: 5

Paulo Almeida
Paulo Almeida

Reputation: 8061

replace is a string method, you can't use it inside the string you're formatting. You would have to do:

s = "A {} string".format('very nice'.replace(' ', '_'))

if you wanted to replace only within the variable (which seems like your objective), or:

s = "A {} string".format('very nice').replace(' ', '_')

if you wanted to replace globally.

Regarding better ways of doing it, I would use os.path.splitext to split the extension, generate the time stamp already in the correct format (if you can control that, of course), instead of using the replace, and do all of that replacing before the os.rename part, so it's more readable (i.e. store the correct strings in variables and use those in the format, instead of doing it all in one line).

Upvotes: 0

Related Questions