Home > Blockchain >  FileNotFoundError: [Errno 2] No such file or directory: '2022_06_02/10/22_17_06.txt' error
FileNotFoundError: [Errno 2] No such file or directory: '2022_06_02/10/22_17_06.txt' error

Time:02-12

Code trials:

def save_data(data):

    name = f"{dt.now().strftime('%Y_%M_%D_%H_%M')}.txt"
    with open(name, 'w') as file:
       file.write(data)

I have this function that should save scraped data to a txt file but I keep getting:

FileNotFoundError: [Errno 2] No such file or directory: '2022_06_02/10/22_17_06.txt'

Is there a way to fix this?

CodePudding user response:

This error message...

FileNotFoundError: [Errno 2] No such file or directory: '2022_06_02/10/22_17_06.txt'

...implies that your system was unable to locate the file 2022_06_02/10/22_17_06.txt.

This line of code:

from datetime import datetime as dt

name = f"{dt.now().strftime('%Y_%M_%D_%H_%M')}.txt"

does creates the variable name with the value 2022_06_02/10/22_17_06.txt but atleast my localhost which is based won't allow to create any file by the name 2022_06_02/10/22_17_06.txt as it is a invalid filename.

Snapshot:

invalid_filename

  • Related