首页 > 解决方案 > Create folders as a function parameter in python

问题描述

In this code i am reading multiple folders and sent the files of each folder as input parameter to processing it in func2(), i need to save the output of processing in new creating folder how can i do that. my problem in this my code is just one folder is created and then the contents of the new folder passes again to func2()!

pp=1
directory = "out"+str(pp) 
parent_dir = 'C:\\Users\\lap\\Desktop\\main\\enc\\'
path = os.path.join(parent_dir, directory )   
os.mkdir(path)  
print("Directory '% s' created" %directory)  

input_path = Path(Path.home(), "Desktop", "main")
for root, dirs, files in os.walk(input_path):
    for file in files:
        file_path = Path(root, file)
        #func(input files, output files)
        cc = func(file_path,'C:\\Users\\lap\\Desktop\\main\\enc\\%s\\' %directory)
        cc.func2()
    pp+=1 

标签: python-3.xfilemkdir

解决方案


只创建一个文件夹的原因是,变量的值在循环directory内永远不会改变。for您可以在代码中添加这些行以获得您想要的内容。

...
for root, dirs, files in os.walk(input_path):
    directory = "out"+str(pp) 
    for file in files:
...

推荐阅读