简体   繁体   English

如何使用Python将文件从一个子文件夹处理到每个目录中的另一个子文件夹?

[英]How to process files from one subfolder to another in each directory using Python?

I have a basic file/folder structure on the Desktop where the "Test" folder contains "Folder 1", which in turn contains 2 subfolders: 我在桌面上有一个基本的文件/文件夹结构,其中“Test”文件夹包含“Folder 1”,后者又包含2个子文件夹:

  • An "Original files" subfolder which contains shapefiles (.shp). “原始文件”子文件夹,其中包含shapefile(.shp)。
  • A "Processed files" subfolder which is empty. “已处理文件”子文件夹为空。

I am attempting to write a script which looks into each parent folder ( Folder 1 , Folder 2 etc) and if it finds an Original Files subfolder, it will run a function and output the results into the Processed files subfolder. 我正在尝试编写一个查看每个父文件夹( 文件夹1文件夹2等)的脚本,如果找到原始文件文件夹,它将运行一个函数并将结果输出到Processed files文件夹中。

I made a simple diagram to showcase this where if Folder 1 contains the relevant subfolders then the function will run; 我做了一个简单的图表来展示如果文件夹1包含相关的子文件夹,那么函数将运行; if Folder 2 does not contain the subfolders then it's simply ignored: 如果文件夹2 包含子文件夹,然后它只是忽略:

文件夹结构

I looked into the following posts but having some trouble: 我调查了以下帖子,但遇到了一些麻烦:

The following is the script which seems to run happily, annoying thing is that it doesn't produce an error so this real noob can't see where the problem is: 以下是似乎运行愉快的脚本,令人讨厌的是它不会产生错误,所以这个真正的菜鸟无法看到问题出在哪里:

import os, sys

from os.path import expanduser
home = expanduser("~")

for subFolders, files in os.walk(home + "\Test\\" + "\*Original\\"):
 if filename.endswith('.shp'):

    output = home + "\Test\\" + "\*Processed\\" + filename

    # do_some_function, output  

I guess you mixed something up in your os.walk() -loop. 我猜你在os.walk() loop中混合了一些东西。

I just created a simple structure as shown in your question and used this code to get what you're looking for: 我刚刚创建了一个简单的结构,如您的问题所示,并使用此代码来获取您正在寻找的内容:

root_dir = '/path/to/your/test_dir'
original_dir = 'Original files'
processed_dir = 'Processed files'

for path, subdirs, files in os.walk(root_dir):
    if original_dir in path:
        for file in files:
            if file.endswith('shp'):
                print('original dir: \t' + path)
                print('original file: \t' + path + os.path.sep + file)
                print('processed dir: \t' + os.path.sep.join(path.split(os.path.sep)[:-1]) + os.path.sep + processed_dir)
                print('processed file: ' + os.path.sep.join(path.split(os.path.sep)[:-1]) + os.path.sep + processed_dir + os.path.sep + file)
                print('')

I'd suggest to only use wildcards in a directory-crawling script if you are REALLY sure what your directory tree looks like. 我建议只在目录爬行脚本中使用通配符,如果你真的确定你的目录树是什么样的。 I'd rather use the full names of the folders to search for, as in my script. 我宁愿使用文件夹的全名来搜索,就像在我的脚本中一样。

Update: Paths 更新:路径

Whenever you use paths, take care of your path separators - the slashes. 无论何时使用路径,都要处理路径分隔符 - 斜杠。

On windows systems, the backslash is used for that: 在Windows系统上,反斜杠用于:

C:\any\path\you\name

Most other systems use a normal, forward slash: 大多数其他系统使用正常的正斜杠:

/the/path/you/want

In python, a forward slash could be used directly, without any problem: 在python中,可以直接使用正斜杠,没有任何问题:

path_var = '/the/path/you/want'

...as opposed to backslashes. ...而不是反斜杠。 A backslash is a special character in python strings. 反斜杠是python字符串中的特殊字符。 For example, it's used for the newline-command: \\n 例如,它用于newline-command: \\n

To clarify that you don't want to use it as a special character, but as a backslash itself, you either have to "escape" it, using another backslash: '\\\\' . 为了澄清你不想将它用作特殊字符,但作为反斜杠本身,你要么必须使用另一个反斜杠“逃避”它: '\\\\' That makes a windows path look like this: 这使得windows路径看起来像这样:

path_var = 'C:\\any\\path\\you\\name'

...or you could mark the string as a "raw" string (or "literal string") with a proceeding r . ...或者您可以将字符串标记为具有前进r的“原始”字符串(或“文字字符串”)。 Note that by doing that, you can't use special characters in that string anymore. 请注意,通过这样做,您不能再在该字符串中使用特殊字符。

path_var = r'C:\any\path\you\name'

In your comment, you used the example root_dir = home + "\\Test\\\\" . 在您的评论中,您使用了示例root_dir = home + "\\Test\\\\" The backslash in this string is used as a special character there, so python tries to make sense out of the backslash and the following character: \\T . 这个字符串中的反斜杠用作特殊字符,因此python尝试从反斜杠和后面的字符中理解: \\T I'm not sure if that has any meaning in python, but \\t would be converted to a tab-stop. 我不确定它在python中是否有任何意义,但\\t Either way - that will not resolve to the path you want to use. 无论哪种方式 - 都无法解析为您要使用的路径。

I'm wondering why your other example works. 我想知道为什么你的另一个例子有效。 In "C:\\Users\\me\\Test\\\\" , the \\U and \\m should lead to similar errors. "C:\\Users\\me\\Test\\\\"\\U\\m应该导致类似的错误。 And you also mixed single and double backslashes. 而你还混合了单反和双反斜杠。

That said... 那说......

When you take care of your OS path separators and trying around with new paths now, also note that python does a lot of path-concerning things for you. 当你现在处理你的OS路径分隔符并尝试使用新路径时,还要注意python为你做了很多与路径相关的事情。 For example, if your script reads a directory, as os.walk() does, on my windows system the separators are already processed as double backslashes. 例如,如果您的脚本读取目录,就像os.walk()那样,在我的Windows系统上,分隔符已经被处理为双反斜杠。 There's no need for me to check that - it's usually just hardcoded strings, where you'll have to take care. 我没有必要检查它 - 它通常只是硬编码的字符串,你必须要小心。

And finally: The Python os.path module provides a lot of methods to handle paths, seperators and so on. 最后: Python os.path模块提供了许多处理路径,分隔符等的方法。 For example, os.path.sep (and os.sep , too) wil be converted in the correct seperator for the system python is running on. 例如, os.path.sep (和os.sep也将)在正确的seperator中转换为运行的系统python。 You can also build paths using os.path.join() . 您还可以使用os.path.join()构建路径。

And finally: The home-directory 最后:主目录

You use expanduser("~") to get the home-path of the current user. 您使用expanduser("~")来获取当前用户的主路径。 That should work fine, but if you're using an old python version, there could be a bug - see: expanduser("~") on Windows looks for HOME first 这应该工作正常,但如果你使用旧的python版本,可能会有一个错误 - 请参阅: Windows上的expanduser(“〜”)首先查找HOME

So check if that home-path is resolved correct, and then build your paths using the power of the os -module :-) 因此,请检查主路径是否已正确解析,然后使用os -module的强大功能构建路径:-)

Hope that helps! 希望有所帮助!

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

相关问题 使用python将文件从一个目录转移到另一个目录 - Transfer files from one directory to another if they match a condition using python 使用 Python 将所有文件从一个目录移动到另一个目录 - Moving all files from one directory to another using Python 将文件从一个目录复制到另一个python - Copying files from one directory to another python 如何将目录中子文件夹中的文件移动到python中的另一个目录? - How do I move files within a subfolder in a directory to another directory in python? 如何在Python 3中将类从一个子文件夹导入另一个子文件夹? - How to import class from one subfolder into another in Python 3? 将一些文件从子文件夹移动到 Python 中的另一个子文件夹 - Moving some files from subfolder to another subfolder in Python 如何列出目录中的文件并逐一处理? -Python - how to make list of files in directory and process them one by one? - Python Python->复制目录,而另一个进程从/向其中删除/添加文件 - Python -> Copy directory while another process removes/adds files from/to it 使用python处理文件目录 - process a directory of files using python 使用 Pyspark 将文件从 HDFS 中的一个目录移动到另一个目录 - Moving files from one directory to another directory in HDFS using Pyspark
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM