Python create file in certain directory with open() -
my code:
boyka = "hello" f = open("~/desktop/" + boyka + ".txt", "a") f.write(boyka) f.close
result:
ioerror: [errno 2] no such file or directory: '~/desktop/hello.txt'
shouldn't script create file since it's "a" ? how can fix code?
i'm using ubuntu.
open()
function not automatically expand ~
users home directory. instead trying create in directory name. guessing not want. in case should use - os.path.expanduser()
, expand ~
user's home directory. example -
import os.path f = open(os.path.expanduser(os.path.join("~/desktop",boyka + ".txt")), "a")
i suggest use os.path.join()
create paths , rather manually creating them.
Comments
Post a Comment