我正在尝试通过从数据框中的特定列读取每一行来创建文件夹:
import numpy as np
import pandas as pd
import os
raw_df = pd.read_excel('C:/Users/home/Desktop/test/Inventory.xls')
df = raw_df[["Barcode", "Brand", "product_name"]]
raw_df:快照
df:快照
我当前的工作目录:C:\Users\home\Desktop\test\
path = os.getcwd()
new_path = os.chdir(path + "\\products\\")
print ("The new working directory is %s" % os.getcwd())
新的工作目录是 C:\Users\home\Desktop\test\products\
for i in range(len(df)):
row = df.iloc[i]
barcode = row["Barcode"]
brand = row["Brand"]
product_name = row["Product_Name"]
if(("\\" + barcode + "\\__" + brand + "\\__" + product_name + "\\") == False):
os.makedir("\\" + barcode + "\\__" + brand + "\\__" + product_name + "\\")
else:
print("failed")
我的输出:
失败失败失败失败失败失败失败
我在原始 df 中有超过 400 行,我只是在测试前 6 行。
我想要实现的目标如下:最终结果快照
所以我试图获取每一行并使用它们创建一个新目录,由双下划线“__”分隔。
附加说明:我已经从工作目录(我自己创建的文件夹)中删除了文件夹并运行了上面的代码,但仍然没有达到我想要的结果。
我会创建一个 bash 脚本,但这不是一个选项,因为在我遍历数据帧时,我需要用各种数据(例如图像等)填充这些文件夹。
任何帮助将不胜感激。如果我能够破解这个问题,我将继续为此工作,并为每个人的利益报告一个解决方案。
慕田峪4524236
茅侃侃
相关分类