Skip to content Skip to sidebar Skip to footer

With Open() Error 22 (windows Path)

I have trouble getting the following code to work: path = 'C:\\Users\\jiversen\\Documents\\Jsons\\'+jsonName+'.json' with open(path,'w') as outfile: json.dump(df,outfile)

Solution 1:

Windows does not allow a colon (:) in file names.

Yes, it's Win 7

Try

import os

jsonName = '2018.04.06-12.00.00.000'
#                        ^  ^  No colons!

path = r'C:\Users\jiversen\Documents\Jsons'
file_name = '{}.json'.format(jsonName)
full_path = os.path.join(path, file_name)

print(full_path)
# C:\Users\jiversen\Documents\Jsons\2018.04.06-12.00.00.000.json

with open(full_path, 'w') as outfile:
    json.dump(df, outfile)

Solution 2:

Look at your error message:

OSError: [Errno 22] Invalid argument: 
'C:\\Users\\jiversen\\Documents\\Jsons\\2018.04.06-12:00:00.000.json'

You are trying to open a file with : in the name. That is not a valid Windows filename.


Post a Comment for "With Open() Error 22 (windows Path)"