Python - input of file path

坚强是说给别人听的谎言 提交于 2020-01-17 12:49:48

问题


this code works fine when I put the path of the file myself. but when I want to get it from users raw_input() it doesn't work. what can I do?

import string
import random

print "enter number between 6 and 20"
n = raw_input()
print "enter pathway of file"
p = raw_input() 

print "creating a new text file"
new_file = open(p, "w")  #the error on this line
m = int(n)


print random.choice(string.ascii_lowercase)

for i in range(0,m):
    for j in range(0,m):
        new_file.write(random.choice(string.ascii_lowercase))
    new_file.write("\n")

回答1:


From the error message you quoted, it looks like you're adding doublequotes to your user input and escaping the backslashes. Don't do that.

Instead of entering

"c:\\Users \\USER\\Desktop\\bar.txt"

at the prompt, enter:

C:\users\USER\Desktop\bar.txt


来源:https://stackoverflow.com/questions/30308309/python-input-of-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!