с open () ошибка 22 (путь Windows) - PullRequest
       4

с open () ошибка 22 (путь Windows)

0 голосов
/ 25 апреля 2018

У меня проблемы с получением следующего кода для работы:

path = "C:\\Users\\jiversen\\Documents\\Jsons\\"+jsonName+'.json'

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

Я получаю следующую ошибку:

OSError                                   Traceback (most recent call last)
<ipython-input-162-ad4856eeb7ee> in <module>()
      7 #path = r "C:\Users\jiversen\Documents\CosmosDB Simulator\Jsons\"+jsonName+'.json'
      8 
----> 9 with open(abs_file_path,'w') as outfile:
     10         json.dump(df,outfile)

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

Я пробовал следующие способы создания пути:

1

path = "C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'

это приводит к следующей ошибке:

File "<ipython-input-166-405d2aae6e9c>", line 7
    path = "C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
                                                                ^
SyntaxError: EOL while scanning string literal

2

path = R"C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'

это приводит к ошибке фоллинга:

File "<ipython-input-167-ff5d5da61135>", line 7
    path = R"C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
                                                                 ^
SyntaxError: EOL while scanning string literal

3

    import os
    script_dir = os.path.abspath('C:\\Users\\jiversen\\Documents\\jsons\\') # i.e. /path/to/dir/foobar.py
    rel_path = 'Jsons\\'+jsonName+'.json'
    abs_file_path = os.path.join(script_dir, rel_path)

это приводит к ошибке фоллинга:

OSError                                   Traceback (most recent call last)
<ipython-input-168-d286c1f58b6a> in <module>()
      7 #path = R"C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
      8 
----> 9 with open(path,'w') as outfile:
     10         json.dump(df,outfile)

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

4 без subdir и без пробелов

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

это приводит к следующей ошибке:

OSError                                   Traceback (most recent call last)
<ipython-input-173-818eaaa66077> in <module>()
      6 path = 'C:\\Users\\jiversen\\Documents\\Jsons\\'+jsonName+'.json'
      7 
----> 8 with open(jsonName+'.json','w') as outfile:
      9         json.dump(df,outfile)

OSError: [Errno 22] Invalid argument: '2018.04.06-12:00:00.000.json'

Дополнительная информация

Я скопировал вставленный путь и убедился, что он существует снова и снова (C: \ Users \ jiversen \ Documents \ Jsons)

Я работаю в Jupyter-ноутбуке

Ответ на глубинное пространство

OSError                                   Traceback (most recent call last)
<ipython-input-177-f09b6b3b06fe> in <module>()
     10 # C:\Users\jiversen\Documents\Jsons\2018.04.06-12:00:00.000.json
     11 
---> 12 with open(full_path, 'w') as outfile:
     13     json.dump(df, outfile)

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

Ответы [ 2 ]

0 голосов
/ 25 апреля 2018

Посмотрите на ваше сообщение об ошибке:

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

Вы пытаетесь открыть файл с : в имени. Это не допустимое имя файла Windows.

0 голосов
/ 25 апреля 2018

Windows не допускает двоеточие (:) в именах файлов.

Yes, it's Win 7

Попробуйте

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...