繁体   English   中英

来自python的Linux命令在子文件夹内执行

[英]Linux command from python to execute inside subfolders

这是我的文件夹结构:

ORDERNO'S  YEAR'S  MONTH'S  DATE'S  CSVFILES
408------->2010---->01-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
               ----->02-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
               ...
    ------->201101-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
               ----->02-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
    ------->201201-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
               ----->02-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
               ...
    ------->2013--01-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
               ----->02-->21--->1.CSV
                           --->2.CSV
                      --->22--->1.CSV
                           --->2.CSV
               ...

由于它们在特定日期内有许多 csv 文件。 我想将每个日期文件夹中的所有 csv 文件与第一个文件的标题合并为一个文件,名称为 orderno_year_month_date.csv。 意味着每个日期文件夹将只有一个以它们的父文件夹命名的 csv。并再次进入文件夹并手动执行命令。

**同样的问题在 3 个月前发布在 askubuntu 上,但那里没有答案。

预期的结构应该是这样的

ORDERNO'S  YEAR'S  MONTH'S  DATE'S  CSVFILES
408------->2010---->01-->21--->408_2010_01_21.CSV
                      --->22--->408_2010_01_22.CSV
                      ...

以前我要去每个订单号的每年文件夹的每个月文件夹的每个日期文件夹。 并用于为标题的单个文件运行此命令。

awk '(NR == 1) || (FNR > 1)' *.csv > 4011_2020_07_16.csv  (example)

我将使用这个模拟文件结构(使用tree命令绘制,并保存在我的电脑中的~/test/下):

test
└── 408
    └── 2010
        └── 01
            ├── 21
            │   ├── 1.csv
            │   └── 2.csv
            ├── 22
            │   ├── 1.csv
            │   └── 2.csv
            └── 23
                ├── 1.csv
                └── 2.csv

您可以在pathlib的帮助下使用 Python 重命名文件,并使用pandas连接它们:

import pandas as pd

from pathlib import Path

def getfolders(files):
    return sorted(list(set([file.parent for file in files])))

def getpathproperty(folder, prop):
    properties = {"orderno": 3, "year": 2, "month": 1, "day": 0}
    for i in range(properties[prop]):
        folder = folder.parent
    return folder.stem

path = Path("~/test").expanduser()
allfiles = list(path.rglob("*.csv")) # Each file in allfiles is a Path object

folders = getfolders(allfiles)

for folder in folders:
    files = sorted(list(folder.glob("*.csv")))
    df = pd.concat([pd.read_csv(file) for file in files])

    # Get the values from the path to rename the files
    orderno = getpathproperty(folder, "orderno")
    year = getpathproperty(folder, "year")
    month = getpathproperty(folder, "month")
    day = getpathproperty(folder, "day")

    # Save the new CSV file
    df.to_csv(folder/f"{orderno}_{year}_{month}_{day}.csv", index=False)

    # Delete old files, commented for safety
    # for file in files:
        # file.unlink(missing_ok=True)

这产生:

test
└── 408
    └── 2010
        └── 01
            ├── 21
            │   └── 408_2010_01_21.csv
            ├── 22
            │   └── 408_2010_01_22.csv
            └── 23
                └── 408_2010_01_23.csv

暂无
暂无

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

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