I\'m writing this simple code:
file = input(\'File to read: \')
fhand = open(file, \'r\')
The file I want to open is called \'test.txt\', and i
you'll have no problems with Python 3 with your exact code (this issue is often encountered when passing windows literal strings where the r
prefix is required).
With python 2, first you'll have to wrap your filename with quotes, then all special chars will be interpreted (\t
, \n
...). Unless you input r"DB\test.txt"
using this raw prefix I was mentionning earlier but it's beginning to become cumbersome :)
So I'd suggest to use raw_input
(and don't input text with quotes). Or python version agnostic version to override the unsafe input
for python 2 only:
try:
input = raw_input
except NameError:
pass
then your code will work OK and you got rid of possible code injection in your code as a bonus (See python 2 specific topic: Is it ever useful to use Python's input over raw_input?).