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.
答案 0 :(得分:1)
您可以使用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的副本。