how do i fix this invalid arguement error

Steve D'Aprano steve+python at pearwood.info
Sat Nov 26 19:06:48 EST 2016


On Sun, 27 Nov 2016 03:12 am, junkone1 at gmail.com wrote:

> import csv
> with open('\\192.168.0.1\fe18cb0618cabd41\ninjatrader$EURUSDTestRun
> 2016-11-25-11-11.csv','r') as f:

\f inserts a FORMFEED character, ASCII code 12. \n inserts a LINEFEED
character, or newline, ASCII code 10. I believe that on Windows, both are
illegal in file names. And \\ inserts a single backslash.

You have three solutions:

(1) Use forward slashes, which Windows accepts as well:

'//192.168.0.1/fe18cb0618cabd41/ninjatrader$EURUSDTestRun
2016-11-25-11-11.csv'


(2) Use a "raw string" to disable most (but not all) backslash escapes:

r'\\192.168.0.1\fe18cb0618cabd41\ninjatrader$EURUSDTestRun
2016-11-25-11-11.csv'


The problem with raw strings is that you cannot end a string with a
backslash, so you cannot write:

# this gives a syntax error
r'C:\My Documents\directory\'


(3) Or escape all your backslashes with more backslashes:

'\\\\192.168.0.1\\fe18cb0618cabd41\\ninjatrader$EURUSDTestRun
2016-11-25-11-11.csv'




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list