ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 19:56:37

ΪʲôĿ¼Ãû³ÆÎÞЧÄØ

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)

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'

yuxijian2020 ·¢±íÓÚ 2021-4-21 20:09:39

if os.path.isfile(each_file):   # ÄãÔÚÕâÀïµÄÅжÏÊÇÈç¹ûÊÇÎļþÔò:
            file_find(each_file, target)
            os.chdir(os.pardir)

È»ºóÄãҪǿÐжÔÒ»¸öÎļþos.chdir   Õâ¿Ï¶¨»á±¨´í°¡   ËüÊÇÎļþ²»ÊÇĿ¼

ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 20:19:17

yuxijian2020 ·¢±íÓÚ 2021-4-21 20:09
È»ºóÄãҪǿÐжÔÒ»¸öÎļþos.chdir   Õâ¿Ï¶¨»á±¨´í°¡   ËüÊÇÎļþ²»ÊÇĿ¼

ÎÒ°Ñ os.chdir ɾµôÁËÒ²»¹ÊÇ»á Ŀ¼Ãû³ÆÎÞЧ

yuxijian2020 ·¢±íÓÚ 2021-4-21 20:39:17


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)

ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 20:52:40

yuxijian2020 ·¢±íÓÚ 2021-4-21 20:39


Ã÷°×ÁË£¬ÄÇÈç¹ûÒª¸ÄÔõô¸ÄÄØ£¬Âé·³ÄãÁË

yuxijian2020 ·¢±íÓÚ 2021-4-21 20:57:32

ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 20:52
Ã÷°×ÁË£¬ÄÇÈç¹ûÒª¸ÄÔõô¸ÄÄØ£¬Âé·³ÄãÁË

ÅжϸÄÁË°¡£¬Äã¸Ä³ÉÅÐ¶Ï ÊDz»ÊÇĿ¼

if os.path.isdir(each_file):

ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 21:09:37

yuxijian2020 ·¢±íÓÚ 2021-4-21 20:57
ÅжϸÄÁË°¡£¬Äã¸Ä³ÉÅÐ¶Ï ÊDz»ÊÇĿ¼

´úÂëÄÜÔËÐÐÁË£¬Ð»Ð»Äã Ô­À´ÊÇÕâÑù

yuxijian2020 ·¢±íÓÚ 2021-4-21 21:13:37

ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 21:09
´úÂëÄÜÔËÐÐÁË£¬Ð»Ð»Äã Ô­À´ÊÇÕâÑù

ÄãÒªÃ÷°×ÄãΪʲôȥµÝ¹é£¬ÊDz»ÊÇÒòΪÅöµ½ÎļþÁËÈ¥µÝ¹é
¶øÊÇÅöµ½Îļþ¼ÐÁË£¬Òª½øµ½Îļþ¼ÐÄÚ¼ÌÐøËÑË÷ËùÒÔ²ÅʹÓõݹé

ÊÇBB°¡ ·¢±íÓÚ 2021-4-21 21:26:15

yuxijian2020 ·¢±íÓÚ 2021-4-21 21:13
ÄãÒªÃ÷°×ÄãΪʲôȥµÝ¹é£¬ÊDz»ÊÇÒòΪÅöµ½ÎļþÁËÈ¥µÝ¹é
¶øÊÇÅöµ½Îļþ¼ÐÁË£¬Òª½øµ½Îļþ¼ÐÄÚ¼ÌÐøËÑË÷ËùÒԲŠ...

ÎÒÃÔºýÁË°ÑÎļþ¼ÐÏë³É file   ¶øÆäʵËûÊÇ dir
Ò³: [1]
²é¿´ÍêÕû°æ±¾: ΪʲôĿ¼Ãû³ÆÎÞЧÄØ