0

im trying to make a program that is password protected and once the password is entered correctly it opens a file. when i try to run the program i get a syntax error saying "EOL while scanning string literal" where i put the file path'

heres my code.

import os
while True:
    print("9. exit")
    print("0. close")
    choice = int(input("enter password: "))
    choice = int(input("enter option: "))
    if (choice>=1124):
        if choice ==1124:
                os.starfile('C:\restriced_access\')

os.starfile('C:\restriced_access\') this is where the problem is. its supposed to open a file folder in my Windows(C:)

Kai laugen
  • 31
  • 5

1 Answers1

1

On my windows PC all of following option were successful in opening a folder.

>>> os.startfile("C:\\testfolder")
>>> os.startfile("C://testfolder")
>>> os.startfile("C:\testfolder")
>>> os.startfile("C:/testfolder")
>>>

And you have a typo in following line.

os.starfile('C:\restriced_access\')

its os.startfile not os.starfile. You are missing a t in startfile.

EDIT

Following works with escaping

>>> os.startfile('C:\\testfolder\\')
>>> os.startfile('C://testfolder//')

Following works without escaping

>>> os.startfile('C:/testfolder/')

Following throws string literal error. So you may want to try one of above.

>>> os.startfile('C:\testfolder\')

SyntaxError: EOL while scanning string literal
>>> 
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • Welcome to SO. It happens to me all the time. If my answer solves the problem do consider accepting by clicking on check mark next to the question so that question doesn't linger a unanswered. – Anil_M Jan 18 '18 at 21:29
  • 1
    @Anil_M Downvoted because your answer still contains the escaping issue + you didn't explain what was wrong with his string. – user4020527 Jan 18 '18 at 21:30
  • does the operating system affect it? its still not working. im not getting a syntex error it just isnt working – Kai laugen Jan 18 '18 at 21:32
  • whats wrong with the string? @JacobWood – Kai laugen Jan 18 '18 at 21:36
  • @JacobWood: Thanks for pointing it out. I was just trying without subfolders. OP's option does dropout and this has been corrected in answer. – Anil_M Jan 18 '18 at 21:38
  • @Kailaugen See my comment on your question - you need to escape backslashes. – user4020527 Jan 18 '18 at 21:38
  • @ Kai laugen, Yes language does affect. On windows system single back-slash `\\` will not work as its used for escaping chars. I believe it can work on linux. – Anil_M Jan 18 '18 at 21:41
  • it didnt quite work but im sure its user error thank you for your help guys – Kai laugen Jan 18 '18 at 21:48
  • No this does **not** work on other operating systems. String interpretation is Python's job, not that of the OS. – Jongware Jan 18 '18 at 22:33
  • what do you mean? – Kai laugen Jan 18 '18 at 22:43
  • @usr2564301: I was talking about './' method of addressing folders. It works on linux (just tested). So syntax like`os.chdir('./test')` will work on linux while not on windows. On the otherhand syntax like os.startfile() will not work on linux. Hope this clarifies context. – Anil_M Jan 18 '18 at 22:53