【发布时间】:2014-04-08 13:54:14
【问题描述】:
下面是我试图用来将目录路径作为用户的“原始输入”的代码的 sn-p。从用户获取输入后,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\larece.johnson\Desktop\Python Programs\Hello World 2", line 14, in <module>
f = open(str,"r+") #I open the text file here which the user gave me
IOError: [Errno 2] No such file or directory: 'C:/Users/larece.johnson/Desktop/Python Programs/BostonLog.log.2014-04-01'
忽略我在下面所做的事情,是否有一种特殊的方式我应该从用户那里获取路径以便 Python 接受它?
比如我要查找的目录和文件是
C:/Users/larece.johnson/Desktop/Python Programs/BostonLog.log.2014-04-01
import re #this library is used so that I can use the "search" function
import os #this is needed for using directory paths and manipulating them
str ="" #initializing string variable for raw data input
#print os.getcwd()
#f = open("C:/Users/larece.johnson/Desktop/BostonLog.log.2014-04-02.log","r+")
str = raw_input("Enter the name of your text file - please use / backslash when typing in directory path: "); #User will enter the name of text file for me
f = open(str,"r+")
【问题讨论】:
-
顺便说一句:
/是斜线;反斜杠是\。