pathlib PurePosixPath

Sayth Renshaw flebber.crue at gmail.com
Tue Oct 10 03:44:20 EDT 2017


> > Hi
> >
> > How do I create a valid file name and directory with pathlib?
> >
> > When I create it using PurePosixPath I end up with an OSError due to an obvously invlaid path being created.
> 
> You're on Windows. The rules for POSIX paths don't apply to your file
> system, and...
> 
> > OSError: [Errno 22] Invalid argument: 'C:\\Users\\Sayth\\Projects\\results/Warwick Farm2017-09-06T00:00:00.json'
> 
> ... the colon is invalid on Windows file systems. You'll have to
> replace those with something else.
> 
> ChrisA

Thanks. Updated the script. But shouldn't it create the file if it doesn't exist? Which none of them will.

for dates in fullUrl:
    # print(dates)
    time.sleep(0.3)
    r = requests.get(dates)
    data = r.json()
    if data["RaceDay"] is not None:
        a = data["RaceDay"]["MeetingDate"]
        b = a[:7]
        file_name = data["RaceDay"]["Meetings"][0]["VenueName"] + '_' + b + '.json'
        result_path = pathlib.PurePath(r'C:\Users\Sayth\Projects\results', file_name)
        with open(result_path, 'a') as f:
            f.write(data)

##Output
File "C:/Users/Sayth/PycharmProjects/ubet_api_mongo/json_download.py", line 42, in <module>
    with open(result_path, 'a') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Sayth\\Projects\\results\\Warwick Farm_2017-09.json'

Process finished with exit code 1

Cheers

Sayth



More information about the Python-list mailing list