2

I'm unable to create a file in python (full path).

Code:

with open('C:/Users/Kobe/backtra/data/BTC-USDT_25-05-20-19:02:19.json', "w+"):
    pass

Error:

Traceback (most recent call last):
  File ".\createFile.py", line 1, in <module>
    with open("C:/Users/Kobe/backtra/data/BTC-USDT_25-05-20-19:02:19.json", "w+"):
OSError: [Errno 22] Invalid argument: 'C:/Users/Kobe/backtra/data/BTC-USDT_25-05-20-19:02:19.json'
Kobe Janssens
  • 310
  • 2
  • 13
  • Try using `"` and if that doesnt help then try using `//` instead of `/` – John May 25 '20 at 17:10
  • Check that you have the right permissions on the path – norok2 May 25 '20 at 17:11
  • 1
    Does this answer your question? [IOError: \[Errno 22\] invalid mode ('r') or filename: 'c:\\Python27\test.txt'](https://stackoverflow.com/questions/15598160/ioerror-errno-22-invalid-mode-r-or-filename-c-python27-test-txt) – meyer1994 May 25 '20 at 17:12
  • Try adding ```r``` prefix to the path. – sushanth May 25 '20 at 17:12
  • @meyer1994/@John Single forward slash is just fine in windows. @Kobe Please include the entire stack trace – mad_ May 25 '20 at 17:13
  • Check whether the path exists or not. If it exists add prefix 'r' to path i.e. ```'C:/Users/Kobe/backtra/data/BTC-USDT_25-05-20-19:02:19.json'``` – SUDHEER TALLURI May 25 '20 at 17:14
  • @SUDHEERTALLURI No, I want to create this file. – Kobe Janssens May 25 '20 at 17:15
  • @John There is no difference between `'` and `"`. `/` is not a special symbol, there is no need to repeat it. – DYZ May 25 '20 at 17:21
  • Oh sorry, I didn't notice w+ parameter. If prefix r doesn't work. check for the permissioins. Try running your console as administrator or change the permissions in the system. – SUDHEER TALLURI May 25 '20 at 17:21
  • 1
    What if you try: `"\"C:/Users/Kobe/backtra/data/BTC-USDT_25-05-20-19:02:19.json\""` because windows likes having `"` when given a path – John May 25 '20 at 17:23

1 Answers1

7

Windows file names cannot contain ':'.

If you remove the ':' it should work:

with open('C:/Users/Kobe/backtra/data/BTC-USDT_25-05-20-190219.json', "w+"):
    pass
CanciuCostin
  • 1,773
  • 1
  • 10
  • 25