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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -