Python:os.walk用于扫描指定目录和搜索指定文件的问题

时间:2018-05-22 08:09:36

标签: python-3.x os.walk

我想扫描当前和更深的文件夹以搜索指定的文件。

[〜/ test] $ tree -a Test background

这是我的测试环境。

[〜/测试] $ LS

import os, shutil, sys, getopt, re

def GetOption(argv):

    FileDir = ""
    Roptarget = ""
    Dirtarget=[]
    try:
        opts, args = getopt.getopt(argv, "hD:F:",["FileDir=", "Roptarget="])
    except getopt.GetoptError:
        print ('Error arg input -D <FileDir> -F <Roptarget>')
        sys.exit(2)

    for opt, arg in opts:
        if opt == "-h":
            print ('Error arg input -D <FileDir> -F <Roptarget>')
            sys.exit()
        elif opt in ("-D", "--FileDir"):
            FileDir = arg
            Dirtarget = FileDir.split("|")
        elif opt in ("-F", "--Roptarget"):
            Roptarget = arg
    return(Dirtarget, Roptarget)

#Below self defined function need update
def detect_walk(file_dir):
    L_0 = []
    L = []
    DirList,Ropfile = GetOption(sys.argv[1:])
    print("DirList = " + str(DirList))
    print("Ropfile = " + Ropfile)
    for root, dirs, files in os.walk(file_dir):
        for file in files:
        L_0.append(file)
        if " ".join(L_0).find(Ropfile):
            print("target rop file = " + Ropfile)
            L.append(os.path.join(root, Ropfile))
    return(L)

if __name__ == '__main__':
    file_path = "/home/test/"
    List = detect_walk(file_path)

以下是我的代码:

python test.py -D "RNCRAM955E|RNCMST954E" -F "^A20180520.1300+0300-1315+0300*RNCMST954E_statsfile.xml$"

我期待输出,为exapmle

如果我输入RNCRAM955E

程序只搜索文件RNCMST954Eif,当找到指定的文件匹配模式时,它将显示目标文件的完整路径。

我是蟒蛇新生。请给我一些建议。谢谢。

1 个答案:

答案 0 :(得分:0)

我已更新我的代码,此代码将启用将指定文件夹中的指定文件转换为另一个文件夹。 像python temp.py -D "RNCMST954E|RNCMST957E|RNCRAM955E" -F "A20180520\.13*"一样使用,但有些代码仍在改进,如您所见,-F之后的文件名应添加转义符号\。那么,我该如何改进呢?

import os, shutil, sys, getopt, re

def GetOption(argv):

    FileDir = ""
    Roptarget = ""
    Dirtarget=[]
    try:
        opts, args = getopt.getopt(argv, "hD:F:",["FileDir=", "Roptarget="])
    except getopt.GetoptError:
        print ('Error arg input -D <FileDir> -F <Roptarget>')
        sys.exit(2)

    for opt, arg in opts:
        if opt == "-h":
            print ('Error arg input -D <FileDir> -F <Roptarget>')
            sys.exit()
        elif opt in ("-D", "--FileDir"):
            FileDir = arg
            Dirtarget = FileDir.split("|")
        elif opt in ("-F", "--Roptarget"):
            Roptarget = arg
    return(Dirtarget, Roptarget)


def detect_walk(file_dir):
    L = []
    desdir = "/home/ekoopgj/ITK/Task/test_folder/"
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if re.search(fileIndex,file) is not None:
                L.append(os.path.join(root, file))
                print("start copy " + os.path.join(root, file) + " to " + desdir)
                shutil.copyfile(os.path.join(root, file),desdir + file)


if __name__ == '__main__':
    DirList,fileIndex = GetOption(sys.argv[1:])
    #use LOOP For and store the file_path varibale as a Formal parameters , can reduce the search time if the target folder contains too many files.
    for dir in DirList:
        file_path = "/home/ekoopgj/ITK/Task/0521/"
        file_path += dir
        print("dir = " + dir)
        List = detect_walk(file_path)