简体   繁体   中英

python automatically string to directory when create a file

I want create a file.My file name like this ->"blabla/blabla" this is not path ,this is a string

and should be "blabla/blabla.txt"

with open(fullnameofjob+".txt", "w+") as f:
                f.write("somethink")

FileNotFoundError: [Errno 2] No such file or directory:'blabla/blabla'

How can I fix that? like this:

If you really want to do this, you could build the name with the unicode value for a division slash and save the file with that:

fullnameofjob = 'blabla' + u'\u2215' + 'blabla'
with open(fullnameofjob+".txt", "w") as f:
  f.write("somethink")

Although this just looks like a slash, it's not actually a slash as such included in file paths and is probably going to cause you problems later down the line. Unfortunately you cannot have / in the file basename on unix or windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM