简体   繁体   English

使用Python以与文件名匹配的文件夹名称递归创建文件夹

[英]Use Python to Create Folders Recursively with Folder Names Matching Filenames

Is there a Python method to create directories recursively? 是否有Python方法来递归创建目录? I have this path: 我有这条路:

/home/data/ with files 'table1.csv', 'table2.csv', ... , 'table1000.csv' in it / home / data /中包含文件“ table1.csv”,“ table2.csv”,...,“ table1000.csv”

I would like to create: /home/data/table1 and move 'table1.csv' in it; 我想创建:/ home / data / table1并在其中移动“ table1.csv”; /home/data/table2 and move 'table2.csv' in it; / home / data / table2并在其中移动“ table2.csv”; . . . /home/data/table1000 and move 'table1000.csv' in it; / home / data / table1000并在其中移动“ table1000.csv”;

The folder names would have to match the csv file names. 文件夹名称必须与csv文件名称匹配。

How can I do it recursively? 我该如何递归呢? I know os.makedirs() should probably be used not sure how it works exactly. 我知道应该使用os.makedirs()不确定它是如何工作的。

Many thanks. 非常感谢。

NOTE: 'table1' and 'table2' etc are just dummy example file names. 注意:“ table1”和“ table2”等只是虚拟示例文件名。 The real filenames are a bit complex. 实际的文件名有点复杂。

Use mkdir to create each dir, from the os library. 使用mkdiros库创建每个目录。

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

For each dir move the current file using shutil.move . 对于每个目录,请使用shutil.move移动当前文件。

How to move a file in Python 如何在Python中移动文件

Each iteration should look like this: 每次迭代应如下所示:

for i in range(1, 1001):
    os.mkdir('/home/data/table' + str(i))
    shutil.move('/home/data/table' + str(i) + '.csv', '/home/data/table' + str(i) + '/table' + str(i) + '.csv')

I would work the following way in Python: 我将以以下方式在Python中工作:

1.Get all files in folder in a list 1.获取列表中文件夹中的所有文件

2.Loop Through the filenames of the list and: 2.浏览列表的文件名并:

  1. Create Folder with the exact name (provided that you do not have duplicates) 创建具有确切名称的文件夹(前提是您没有重复的名称)
  2. Move file in the folder 移动文件夹中的文件
  3. Next File 下一个档案

A simple search in the net would get you ready examples on how to do each step of the above. 在网上进行简单的搜索即可获得有关如何完成上述每个步骤的示例。

Edit: Below a simple example for csv files. 编辑:下面是一个简单的csv文件示例。

import glob, os
import shutil
dir="D:\Dropbox\MYDOCS\DEV\python\snippets"
os.chdir(dir)
for file in glob.glob("*.csv"):
    dst=dir+"\\"+file.replace(" ","_").replace(".csv","")
    os.mkdir(dst)
    print(dst)
    shutil.move(file,dst)

Used windows paths, since I use windows, you ll need to change that to linux paths. 使用的Windows路径,因为我使用Windows,所以您需要将其更改为Linux路径。

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

相关问题 Python:更改所有文件夹和子文件夹中的文件名和文件夹名 - Python: Changing filenames and folder names in all folders and subfolders 使用 glob 参数递归匹配文件名 - Recursively matching filenames with glob argument 如何将具有相同名称的文件夹递归复制到 Google Colab 中的另一个文件夹 - How to copy folders with same names recursively to another folder in Google Colab 根据文件名创建文件夹 - Create folders based on filenames python将文件夹名称附加到所有子文件夹中的文件名 - python append folder name to filenames in all sub folders Python:识别文件夹结构中的数字名称文件夹 - Python: Identifying numerically names folders in a folder structure Python递归删除目录中的文件/文件夹,但不删除父目录或特定文件夹 - Python recursively remove files/folders in a directory, but not the parent directory or a specific folder 如何创建多个带有名称的文件夹,并使用python将多个拉链提取到每个不同的文件夹? - How to create multiple folders with names, and extract multiple zips to each different folder, with python? 在python中使用正则表达式匹配文件名 - Matching filenames with regex in python 在Python中递归读取文件夹 - Reading folders recursively in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM