0
def wpath():
defpath = '/home/me/documents/'
os.chdir(defpath)
path = str(input("would you like to change current working directory? [y/yes] if you want"))
if path == 'yes' or path == 'y':
while True:
try:
os.chdir(input('Enter new path! \n'))
break
except OSError:
print('\nno such directory! Try again!')
else:
pass
src()
def src():
global d
d = {}
n = 0
for file in os.listdir('.'):
if re.search('.?\.(txt|csv|log)', file):
n += 1
print(n, file)
d[n] = file
if len(d) == 0:
del d
wpath()
wpath()
val = int(input('Enter number of file\n'))
if val in d:
source = d[val]
當我發表我的文章後,我意識到我的錯誤。謝謝。 現在的代碼工作。我糾正了我的問題中的代碼。如何刪除循環中函數的變量?
Eli「這就是說,del d是一種代碼味道,我99.99%確定你不需要在這裏使用它,你能解釋一下你準備做什麼嗎?」 我想刪除一個空變量'd',如果在目錄中沒有需要的文件(txt,log) – 2011-05-09 12:53:45
@werwolf_x:你爲什麼要刪除它? – 2011-05-09 13:06:21
我認爲這是最簡單的選擇。你有什麼建議? – 2011-05-09 14:01:27