ΪʲôĿ¼Ãû³ÆÎÞЧÄØ
import osdef file_find(start_dir, target):
os.chdir(start_dir)
for each_file in os.listdir(os.curdir):
if each_file == target:
print(os.getcwd() + os.sep + each_file)
if os.path.isfile(each_file):
file_find(each_file, target)
os.chdir(os.pardir)
start_dir = input('ÇëÊäÈë´ý²éÕҵijõʼĿ¼£º')
target = input('ÇëÊäÈëÐèÒª²éÕÒµÄÄ¿±êÎļþ£º')
file_find(start_dir, target)
>>>
ÇëÊäÈë´ý²éÕҵijõʼĿ¼£ºD:\\python
ÇëÊäÈëÐèÒª²éÕÒµÄÄ¿±êÎļþ£º20.1.py
Traceback (most recent call last):
File "D:\python\ÊÔ×Åд\30.2.py", line 13, in <module>
file_find(start_dir, target)
File "D:\python\ÊÔ×Åд\30.2.py", line 8, in file_find
file_find(each_file, target)
File "D:\python\ÊÔ×Åд\30.2.py", line 3, in file_find
os.chdir(start_dir)
NotADirectoryError: Ŀ¼Ãû³ÆÎÞЧ¡£: 'LICENSE.txt' if os.path.isfile(each_file): # ÄãÔÚÕâÀïµÄÅжÏÊÇÈç¹ûÊÇÎļþÔò:
file_find(each_file, target)
os.chdir(os.pardir)
È»ºóÄãҪǿÐжÔÒ»¸öÎļþos.chdir Õâ¿Ï¶¨»á±¨´í°¡ ËüÊÇÎļþ²»ÊÇĿ¼ yuxijian2020 ·¢±íÓÚ 2021-4-21 20:09
È»ºóÄãҪǿÐжÔÒ»¸öÎļþos.chdir Õâ¿Ï¶¨»á±¨´í°¡ ËüÊÇÎļþ²»ÊÇĿ¼
ÎÒ°Ñ os.chdir ɾµôÁËÒ²»¹ÊÇ»á Ŀ¼Ãû³ÆÎÞЧ
import os
def file_find(start_dir, target):
os.chdir(start_dir) # --------> ÕâÀïÊǹؼü£¬ÏÂÃæÅжÏÊÇÎļþºó£¬µÝ¹éµ½ÕâÀïÈ»ºó±¨´í
for each_file in os.listdir(os.curdir):
if each_file == target:
print(os.getcwd() + os.sep + each_file)
if os.path.isfile(each_file):
file_find(each_file, target)
os.chdir(os.pardir) yuxijian2020 ·¢±íÓÚ 2021-4-21 20:39
Ã÷°×ÁË£¬ÄÇÈç¹ûÒª¸ÄÔõô¸ÄÄØ£¬Âé·³ÄãÁË ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 20:52
Ã÷°×ÁË£¬ÄÇÈç¹ûÒª¸ÄÔõô¸ÄÄØ£¬Âé·³ÄãÁË
ÅжϸÄÁË°¡£¬Äã¸Ä³ÉÅÐ¶Ï ÊDz»ÊÇĿ¼
if os.path.isdir(each_file): yuxijian2020 ·¢±íÓÚ 2021-4-21 20:57
ÅжϸÄÁË°¡£¬Äã¸Ä³ÉÅÐ¶Ï ÊDz»ÊÇĿ¼
´úÂëÄÜÔËÐÐÁË£¬Ð»Ð»Äã ÔÀ´ÊÇÕâÑù ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 21:09
´úÂëÄÜÔËÐÐÁË£¬Ð»Ð»Äã ÔÀ´ÊÇÕâÑù
ÄãÒªÃ÷°×ÄãΪʲôȥµÝ¹é£¬ÊDz»ÊÇÒòΪÅöµ½ÎļþÁËÈ¥µÝ¹é
¶øÊÇÅöµ½Îļþ¼ÐÁË£¬Òª½øµ½Îļþ¼ÐÄÚ¼ÌÐøËÑË÷ËùÒÔ²ÅʹÓõݹé yuxijian2020 ·¢±íÓÚ 2021-4-21 21:13
ÄãÒªÃ÷°×ÄãΪʲôȥµÝ¹é£¬ÊDz»ÊÇÒòΪÅöµ½ÎļþÁËÈ¥µÝ¹é
¶øÊÇÅöµ½Îļþ¼ÐÁË£¬Òª½øµ½Îļþ¼ÐÄÚ¼ÌÐøËÑË÷ËùÒԲŠ...
ÎÒÃÔºýÁË°ÑÎļþ¼ÐÏë³É file ¶øÆäʵËûÊÇ dir
Ò³:
[1]