pathlib PurePosixPath

Sayth Renshaw flebber.crue at gmail.com
Tue Oct 10 03:21:24 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.

import pathlib

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


##Output
C:\Users\Sayth\Anaconda3\envs\json\python.exe C:/Users/Sayth/PycharmProjects/ubet_api_mongo/json_download.py
Traceback (most recent call last):
  File "C:/Users/Sayth/PycharmProjects/ubet_api_mongo/json_download.py", line 40, in <module>
    with open(result_path, 'a') as f:
OSError: [Errno 22] Invalid argument: 'C:\\Users\\Sayth\\Projects\\results/Warwick Farm2017-09-06T00:00:00.json'

Process finished with exit code 1

Not sure exactly which way to fix it.

Cheers

Sayth



More information about the Python-list mailing list