Python-文件路径输入 [英] Python - input of file path

查看:671
本文介绍了Python-文件路径输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我自己放置文件路径时,此代码可以正常工作.但是当我想从用户raw_input()获取它时,它不起作用.我该怎么办?

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")

推荐答案

从您引用的错误消息来看,您似乎在用户输入中添加了双引号并转义了反斜杠.不要那样做.

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.

代替输入

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

在提示符下输入:

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

这篇关于Python-文件路径输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆