Python 将目录路径作为用户输入的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22939211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:01:43  来源:igfitidea点击:

What is the proper way to take a directory path as user input?

pythonraw-input

提问by mrokeowo

Below is a snippet of code I am trying to use to take a directory path as "raw input" from the user. I receive the following error after the input is taken from the user:

下面是一段代码,我试图用来将目录路径作为用户的“原始输入”。从用户那里获取输入后,我收到以下错误:

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'

Ignoring what I have done below, is there a particular way I am supposed to take the path from user so that Python accepts it?

忽略我在下面所做的,是否有一种特殊的方式我应该从用户那里获取路径,以便 Python 接受它?

For example, the directory and file I'm looking for is

例如,我正在寻找的目录和文件是

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

采纳答案by tipanverella

I think you should try something like:

我认为你应该尝试这样的事情:

import sys
import os

user_input = raw_input("Enter the path of your file: ")

assert os.path.exists(user_input), "I did not find the file at, "+str(user_input)
f = open(user_input,'r+')
print("Hooray we found your file!")
#stuff you do with the file goes here
f.close()

回答by Kei Minagawa

It seems you want to check if the directory exists.

您似乎想检查目录是否存在。

If so, see os.path.isdir.

如果是这样,请参阅os.path.isdir

os.path.isdir(path)
    Return True if path is an existing directory.
    This follows symbolic links, so both islink()
    and isdir() can be true for the same path.

You can do like this:

你可以这样做:

s = raw_input();
if os.path.isdir(s):
    f = open(s, "r+")
else:
    print "Directory not exists."

回答by mrokeowo

I figured it out... I forgot to add the file extension at the end of the file name on my directory path; I did not notice that I was cutting it off by just copying/pasting the name of my file.... the program works now... thank you all!

我想通了......我忘记在我的目录路径上的文件名末尾添加文件扩展名;我没有注意到我只是通过复制/粘贴我的文件名来切断它......该程序现在可以运行......谢谢大家!