[Tutor] Variable and a raw string
Mats Wichmann
mats at wichmann.us
Mon Jan 20 15:43:15 EST 2020
On 1/20/20 1:21 PM, Joel Goldstick wrote:
> On Mon, Jan 20, 2020 at 3:15 PM Alessandro Caudilho <caudilho at cock.li> wrote:
>>
>> Hello all
>>
>> my code below:
>>
>> ============================
>> from datetime import datetime
>>
>> state = 'colorado/us'
>>
> the following line is causing your problem. 'state' is 'colorado/us
> so you are trying to open
> /home/user/colorado/us.txt
>
> You could change the / to _ and it should work
>> with open('/home/user/' + state + '.txt', 'a') as f:
>> time = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
>> f.write(time)
>> f.close()
>> ============================
>>
>> And I get FileNotFoundError: [Errno 2] No such file or directory: "/home/user/colorado/us'.txt"
>>
>> So how can I create a variable and assign the raw string to it? Are there other solutions for this problem?
In particular, it's causing a problem because you're asking to create a
file in a directory /home/user/colorado that doesn't exist. If you
follow Joel's suggestion and change state to 'colorado_us', then you're
trying to create the file 'colorado_us.text' in /home/user, which will
work (as long as /home/user exists).
You can also add logic to create necessary directories if you want to
organize your data by directories...
More information about the Tutor
mailing list