简体   繁体   English

在python中使用os.walk在子文件夹中找到文件的正确路径?

[英]Finding correct path to files in subfolders with os.walk with python?

I am trying to create a program that copies files with certain file extension to the given folder. 我正在尝试创建一个程序,将具有某些文件扩展名的文件复制到给定的文件夹。 When files are located in subfolders instead of the root folder the program fails to get correct path. 当文件位于子文件夹而不是根文件夹中时,程序将无法获取正确的路径。 In its current state the program works perfectly for the files in the root folder, but it crashes when it finds matching items in subfolders. 在当前状态下,该程序可以完美地处理根文件夹中的文件,但是当它在子文件夹中找到匹配的项目时,它将崩溃。 The program tries to use rootfolder as directory instead of the correct subfolder. 该程序尝试将rootfolder用作目录,而不是正确的子文件夹。

My code is as follows 我的代码如下

# Selective_copy.py walks through file tree and copies files with
# certain extension to give folder
import shutil
import os
import re

# Deciding the folders and extensions to be targeted
# TODO: user input instead of static values
extension = "zip"
source_folder = "/Users/viliheikkila/documents/kooditreeni/"
destination_folder = "/Users/viliheikkila/documents/test"

def Selective_copy(source_folder):
    # create regex to identify file extensions
    mo = re.compile(r"(\w+).(\w+)")  # Group(2) represents the file extension
    for dirpath, dirnames, filenames in os.walk(source_folder):
        for i in filenames:
            if mo.search(i).group(2) == extension:
                file_path = os.path.abspath(i)
                print("Copying from " + file_path + " to " + destination_folder)
                shutil.copy(file_path, destination_folder)

Selective_copy(source_folder)

dirpathwalk提供的其中一项功能,这是有原因的:它提供了files项目所在目录的路径。您可以使用它来确定您应该使用的子文件夹。

file_path = os.path.abspath(i)

This line is blatantly wrong. 这行公然错了。 Keep in mind that filenames keeps list of base file names. 请记住, filenames保留了基本文件名列表。 At this point it's just a list of strings and (technically) they are not associated at all with files in filesystem. 此时,这只是一个字符串列表,并且(从技术上来说)它们根本不与文件系统中的文件关联。

os.path.abspath does string-only operations and attempts to merge file name with current working dir . os.path.abspath执行仅字符串操作,并尝试将文件名与当前工作目录合并 As a result, merged filename points to file that does not exist . 结果,合并的文件名指向不存在的文件。

What should be done is merge between root and base file name (both values yield from os.walk ): 应该做的是在root文件名和基本文件名之间合并(两个值都来自os.walk ):

file_path = os.path.abspath(dirpath, i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM