這是我的代碼,它是一個基於文本的文件瀏覽器。用戶通過選擇分配的號碼來選擇要瀏覽的驅動器或目錄。當你運行這個腳本時,它將顯示從0到任意數量的項目的輸出。但是,當顯示文件夾的內容時,它將從1開始列出,這會拋出您的選擇。計數從0開始的列表
from os import listdir
import win32api
#need help with this block
def glist(path):
global nlist
nlist = []
for i in listdir(path):
nlist.append(i)
countf=len(nlist)
print str(countf) + " " + str(i)
def getfiles(dir, filename):
for i in listdir(dir):
newtext=open(filename,'a')
newtext.write("\n"+ "\n" + i)
newtext.close()
def getdrives():
global drives
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
for index, item in enumerate(drives):
print index, item
print "Select which drive to work with: "
getdrives()
x = raw_input("Which Drive:")
glist(drives[int(x)])
y = raw_input("Select Folder: ")
glist(drives[int(x)] + nlist[int(y)])
@kojiro不要 - 那是我錯過了 - 好點糾正的職位 –