根据文件夹的条件导入CSV文件,该文件夹中的另一个文件夹中

发布于 2025-02-07 02:04:27 字数 395 浏览 4 评论 0原文

我想根据另一个文件夹中文件夹的条件复制和粘贴CSV文件。

主文件夹的名称是“ main”,其中有文件夹,即main_1,m​​ain_2,main_3,......和main_45。每个文件夹中也有CSV文件,即A1,B2,C3,D4,E5,........................................................................................................................................................................

我只想从文件夹中复制A1.CSV,B2.CSV和C3.CSV,然后将它们粘贴到另一个空文件夹中。对我来说,它看起来太复杂了,但我敢肯定,对于专家来说,这很容易。提前致谢

I would like to copy and paste csv files based on conditions from folders inside another folder.

The main folder's name is “main”, and there are folders inside it, namely main_1, main_2, main_3,......, and main_45. There are also csv files inside each folder, namely, a1, b2, c3, d4, e5,........z30.

I just want to copy a1.csv, b2.csv, and c3.csv from the folders and paste them into another empty folder. It looks too complex to me, but I am sure it would be easy for experts. Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2025-02-14 02:04:28

OS.Walk通过目录进行迭代,并在字符串中显示所有文件。

因此,通过MAIN文件夹进行迭代,如果文件名以.csv结束,则使用shutil.copyfile.copyfile函数

import os, shutil
for p, d, f in os.walk("main"):
    for fname in f:
        if fname.endswith(".csv"):
            shutil.copyfile(os.path.join(p, f), os.path.join("ANOTHER EMPTY FOLDER NAME", f))

docs:

https://docs.python.org/3/library/os.html

=“ https://docs.python.org/3/library/shutil.html” rel =“ nofollow noreferrer”> https://docs.python.org/3/library/library/shutil/shutil/shutil.html

os.walk iterates through directory and show all files in string.

So iterate through main folder and if the file name ends with .csv, then copy it with shutil.copyfile function

import os, shutil
for p, d, f in os.walk("main"):
    for fname in f:
        if fname.endswith(".csv"):
            shutil.copyfile(os.path.join(p, f), os.path.join("ANOTHER EMPTY FOLDER NAME", f))

docs:

https://docs.python.org/3/library/os.html

https://docs.python.org/3/library/shutil.html

情深如许 2025-02-14 02:04:28

如果您希望在某个文件夹和子文件夹下所有CSV文件名,那么一种简单的方法是使用python的 glob.glob() 函数。有了此,您可以为CSV文件指定*。csv,也可以使其重复到子文件夹中:

import glob

for csv_filename in glob.glob(r'C:\main\**\*.csv', recursive=True):
    print(csv_filename)

如果您只想复制启动a1A3 或C10例如,正则表达式可以有所帮助:

import glob
import os
import shutil
import re

re_abc123 = re.compile('[a-z]\d+')

for csv_filename in glob.glob(r'C:\main\**\*.csv', recursive=True):
    basename = os.path.basename(csv_filename)
    
    if re_abc123.match(basename):
        print(csv_filename)
        shutil.copyfile(csv_filename, os.path.join('output_folder', basename))

If you want all CSV filenames under a certain folder and sub-folders, an easy approach would be to use Python's glob.glob() function. With this you can specify *.csv for just the CSV files and also make it recurse into sub-folders:

import glob

for csv_filename in glob.glob(r'C:\main\**\*.csv', recursive=True):
    print(csv_filename)

If you only want to copy CSV filenames which start a1 a3 or c10 for example, a regular expression could help:

import glob
import os
import shutil
import re

re_abc123 = re.compile('[a-z]\d+')

for csv_filename in glob.glob(r'C:\main\**\*.csv', recursive=True):
    basename = os.path.basename(csv_filename)
    
    if re_abc123.match(basename):
        print(csv_filename)
        shutil.copyfile(csv_filename, os.path.join('output_folder', basename))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文