1

I 'm python beginner

I can't execute this code

with open('D:\Machine_learning_student_project\New folder\student-mat.csv','r') as f:
    a = csv.reader(f)

open function syntax 'mode' wont work

but when I change to "/"

with open('D:/Machine_learning_student_project/New folder/student-mat.csv','r') as f:
    a = csv.reader(f)

It just worked

martineau
  • 119,623
  • 25
  • 170
  • 301
Miso_soup
  • 11
  • 1
  • 1
    Please post the error message. I got _...Malformed \N character..._ – tdelaney Jun 10 '22 at 14:17
  • I don't use windows (mac and linux user), but I believe the issue has to do with `\\` being used to escape. – astrochun Jun 10 '22 at 14:18
  • The traditional path separator on UNIX is the forward and on Windows the backward slash. Over time and versions Windows started to accept the forward slash as well, with some limitation of cause. – Klaus D. Jun 10 '22 at 14:19
  • *Windows* doesn't accept it; Python automatically converts it when interacting with the Windows filesystem. – CrazyChucky Jun 10 '22 at 14:20
  • It is "/" my date display 10/6/2565 – Miso_soup Jun 10 '22 at 14:20
  • @CrazyChucky: Most (almost all) Windows APIs accept `/` just fine. You can't use it in a DOS prompt (because `/` is for switches there), but Python is calling the raw WinAPI APIs, which are largely perfectly happy with either `/` or `\ `. Python rarely, if ever, needs to convert them. e.g. [The `CreateFile` docs](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea): "You may use either forward slashes (/) or backslashes (\\) in this name." – ShadowRanger Jun 10 '22 at 14:23
  • @ tdelaney File "", line 5 with open('D:\Machine_learning_student_project\New folder\student-mat.csv','r') as f: ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 35-36: malformed \N character escape – Miso_soup Jun 10 '22 at 14:26
  • You have to use a raw string to prevent the backslashes being interpreted as escape sequences: `r'…'`. – Klaus D. Jun 10 '22 at 14:26
  • thanks a lot :) Klaus D. ShadowRanger CrazyChucky tdelaney tdelaney astrochun – Miso_soup Jun 10 '22 at 14:34

0 Answers0