This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: csv
Type: Stage: resolved
Components: Unicode Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: abarry, berker.peksag, bethard, ezio.melotti, fabian_b, vstinner
Priority: normal Keywords:

Created on 2016-06-20 15:24 by fabian_b, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
MesspunkteAusKurven.py fabian_b, 2016-06-20 15:24 three lines of python code
Messages (3)
msg268904 - (view) Author: Fabian (fabian_b) Date: 2016-06-20 15:24
Hi,

I am running Canopy on Windows 7 64 bit.
When I run the attached file, I get the following error:


----> 5 with open('C:\Users\Anwender\Desktop\Test\blub.txt') as csvfile:
      6     dialekt = csv.Sniffer().sniff(csvfile.read(1024))

IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\Anwender\\Desktop\\Test\x08lub.txt' 


As you see, Python complains about the file name after it changes the file name for no reason.

The thing is, when I use a capital letter after every back slash it is fine, but I needed quite some time to figure that out. So it is very annoying for everyone encountering the problem for the first time. 
Also numbers at the beginning of a file name seem not to work either.

Thank you for developing Python!
msg268905 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-06-20 15:28
Thanks for the report. \t is a tab character. You can use

    'C:\\Users\\Anwender\\Desktop\\Test\\blub.txt'

or

    r'C:\Users\Anwender\Desktop\Test\blub.txt'

or

    'C:/Users/Anwender/Desktop/Test/blub.txt'
msg268906 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-06-20 15:33
(Berker beat me to it, but posting anyway since it is more detailed)

Backslashes are escape characters, and "\b" is treated as "\x08". Invalid combinations (e.g. "\A", "\D"...) automatically escape the backslash, but you shouldn't rely on this behaviour - as you can see, it can break in various ways.

To fix your issue, you can either:

- Add a single 'r' before the beginning of your string, i.e. r"C:\Users\Anwender\Desktop\Test\blub.txt" - the 'r' prefix tells Python to automatically escape all backslashes it sees (unless it's at the end of the string, in that case you need to escape it yourself).

- Escape each backslash in your string, i.e. "C:\\Users\\Anwender\\Desktop\\Test\\blub.txt" - the extra backslash tells Python "treat this as a literal backslash, don't use it to escape anything"

- Since this is Windows, you can use forward slashes, i.e. "C:/Users/Anwnder/Desktop/Test/blub.txt", it will work all the same for Python.
History
Date User Action Args
2022-04-11 14:58:32adminsetgithub: 71543
2016-06-20 15:33:11abarrysetnosy: + abarry
messages: + msg268906
2016-06-20 15:28:06berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg268905

resolution: not a bug
stage: resolved
2016-06-20 15:24:24fabian_bcreate