0

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:

oguz zeyveli
  • 61
  • 1
  • 5

1 Answers1

0

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.

Nordle
  • 2,915
  • 3
  • 16
  • 34
  • ok . I will try "/" replace to specific something and after string.replace("","/") . Thanks for your help – oguz zeyveli Apr 21 '19 at 12:02
  • thats a great idea .Thanks – oguz zeyveli Apr 21 '19 at 12:08
  • 1
    @oguzzeyveli No, this is a really bad idea. Don't use weird unicode characters in file names. It's just going to cause trouble. – jpmc26 Apr 21 '19 at 16:52
  • 1
    even though the solution works, it's a terrible idea to use `\ or /` characters in file names. I would suggest you to have a different structure. You can also check here https://stackoverflow.com/questions/9847288/is-it-possible-to-use-in-a-filename – sertsedat Apr 23 '19 at 10:07
  • Yes it is not a good idea, as indicated by myself and the previous comment. +1 for the link to extra info though. – Nordle Apr 23 '19 at 10:12