从Python中指定的目录中选择文件

从Python中指定的目录中选择文件,python,file,select,directory-listing,Python,File,Select,Directory Listing,我想按列表左侧的数字选择一个文件,但我只能这样做: import os path="/root/Desktop" dirList=os.listdir(path) for fname in dirList: print fname selected = raw_input("Select a file above: ") 我该怎么办 例如: 提前感谢您。您可以尝试以下方法: import os path="/root/Desktop" dirList=os.listdir(path

我想按列表左侧的数字选择一个文件,但我只能这样做:

import os
path="/root/Desktop"
dirList=os.listdir(path)
for fname in dirList:
    print fname

selected = raw_input("Select a file above: ")
我该怎么办

例如:


提前感谢您。

您可以尝试以下方法:

import os
path="/root/Desktop"
dirList=os.listdir(path)
for i in range(0,len(dirList)): # generate an index an loop over it
    print "%d)" % (i+1), dirList[i] # print a selection number matching each file

selected = raw_input("Select a file above: ")
selected = int(selected) # cast the input to int

print "You have selected:", dirList[selected-1] # you can get the corresponding entry!

这应该可以做到:)

您可以尝试以下方法:

import os
path="/root/Desktop"
dirList=os.listdir(path)
for i in range(0,len(dirList)): # generate an index an loop over it
    print "%d)" % (i+1), dirList[i] # print a selection number matching each file

selected = raw_input("Select a file above: ")
selected = int(selected) # cast the input to int

print "You have selected:", dirList[selected-1] # you can get the corresponding entry!
这应该可以做到:)

但是,请注意,没有进行错误更正。您应该捕获输入不是整数的情况


但是,请注意,没有进行错误更正。您应该捕获输入不是整数的情况。

您知道特定索引处的文件名,因此可以执行以下操作:

import os
path="/root/Desktop"
dirList=os.listdir(path)

for index in range(0,len(dirList)):
    print str(index+1) + ": " + dirList[index]

selected = raw_input("Select a file above: ")
print "You selected filename: " + dirList[selected];

编辑:oops to slow

您知道特定索引处的文件名,因此可以执行以下操作:

import os
path="/root/Desktop"
dirList=os.listdir(path)

for index in range(0,len(dirList)):
    print str(index+1) + ": " + dirList[index]

selected = raw_input("Select a file above: ")
print "You selected filename: " + dirList[selected];
编辑:oops减慢速度

尝试以下操作:

import os

listed_files = []
for i, fname in enumerate(os.listdir("/root/Desktop")):
    print i, ': ', fname
    listed_files.append(fname)

selected = raw_input("Select a file above: ")
open("/root/Desktop/%s" % listed_files[int(selected)])
试试这个:

import os

listed_files = []
for i, fname in enumerate(os.listdir("/root/Desktop")):
    print i, ': ', fname
    listed_files.append(fname)

selected = raw_input("Select a file above: ")
open("/root/Desktop/%s" % listed_files[int(selected)])

创建一个字典,其中键是索引,值是文件名(使用enumerate(dirList)获得每次迭代的(index,filename)元组)。然后根据输入(键)拉取相应的值。

创建一个字典,其中键是索引,值是文件名(使用enumerate(dirList)获得每次迭代的(索引,文件名)元组)。然后根据输入(键)拉取相应的值。

您应该对列表使用enumerate,然后处理输入错误。理想情况下,这将是一个函数,而不是执行
中断
,您只需返回所选文件即可

import os

path="/root/Desktop"
dirList=os.listdir(path)

for i, fname in enumerate(dirList):
    print "%d) %s" % (i + 1, fname)

while True:
    try:
        selectedInt = int(raw_input("Select a file above: "))
        selected = dirList[selectedInt - 1]
        break
    except Exception:
        print "Error: Please enter a number between 1 and %d" % len(dirList)

您应该对列表使用enumerate,然后处理输入错误。理想情况下,这将是一个函数,而不是执行
中断
,您只需返回所选文件即可

import os

path="/root/Desktop"
dirList=os.listdir(path)

for i, fname in enumerate(dirList):
    print "%d) %s" % (i + 1, fname)

while True:
    try:
        selectedInt = int(raw_input("Select a file above: "))
        selected = dirList[selectedInt - 1]
        break
    except Exception:
        print "Error: Please enter a number between 1 and %d" % len(dirList)

@Boldewyn很好地使用了
enumerate
,以便在列表迭代的同时生成索引。@Boldewyn很好地使用了
enumerate
,以便在列表迭代的同时生成索引。修复了一个部分,它工作得完美无缺。谢谢打印“您选择的文件名:”+dirList[int(selected)-1]——修复了一个部件,它可以完美地工作。谢谢打印“您选择的文件名:”+dirList[int(selected)-1]---