[Tutor] Variable and a raw string
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Jan 20 19:08:31 EST 2020
On 20/01/2020 19:50, Alessandro Caudilho wrote:
> state = 'colorado/us'
>
> with open('/home/user/' + state + '.txt', 'a') as f:
>
> And I get FileNotFoundError: [Errno 2] No such file or directory: "/home/user/colorado/us'.txt"
Notice the error has a spurious ' within it - between /us and .txt.
That quote is not in your code which suggests that there must be some
code you are not showing us?
Eliminate that quote and it should work - assuming you have access
to the directory and that it exists.
Also note that you don't need a close() when you use the with
structure to open the file. close() is done automatically
by Python.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list