csv 中的一列每行都有几个数字:
col
12
14
11
..
我用这个名字为每一行创建了一个文件夹:
import pandas as pd
import os
path = ..
fn = pd.read_csv(r'C:\Users\user\Desktop\xlfile\asc.csv',header = None)
for i in df["col"].astype(str):
os.mkdir(os.path.join(path, i))
os.mkdir(os.path.join(path, i)) #this has to be make in the last folder
#file from a certain path to be copied
现在我想创建一个文件夹,在每个文件夹中都有一个特定的文件。
文件夹名称:new_f
并且文件位于路径中
path: ...
如何创建一个命名文件夹并将new_f
该文件的副本复制到该路径中的每个文件夹?
更新:进一步解释
下面的答案做了什么,但只是这些文件也需要在另一个文件夹中。
例子:
文件夹现在位于答案中的代码之后:
12 // file.shp
14 // file.shp
虽然他们应该
12 // new_folder// file.shp annd file.shx and file.dbf
path = 'this path has the above files scattered so maybe it can read what files are there and copy them to each of the folders as said.
您可以使用shutil
将文件复制到您的目录中:
import pandas as pd
import os
from shutil import copy
path = ..
file_names = ['text1.txt', 'text2.txt', 'text3.txt'] # replace with your file you want copied
df = pd.read_csv(r'C:\Users\user\Desktop\xlfile\asc.csv',header = None)
for i in df["col"].astype(str):
dest = os.path.join(path, i)
os.mkdir(dest)
dest = os.path.join(path, i, 'new_folder')
os.mkdir(dest)
for file_name in file_names:
source = os.path.join(path, file_name)
copy(source, dest)
这会生成 3 个文件夹,分别称为 11、12 和 14,每个文件夹都包含文件 text.txt 的副本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句