Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建文件夹及其文件夹的副本,同时更改其名称_Python_Python 3.x_Os.path - Fatal编程技术网

Python 创建文件夹及其文件夹的副本,同时更改其名称

Python 创建文件夹及其文件夹的副本,同时更改其名称,python,python-3.x,os.path,Python,Python 3.x,Os.path,我正在尝试为在文件夹中的每个文件上运行的批处理脚本创建一个输出文件夹/文件。我希望当我在targetFolder中找到所有文件或文件夹时,我可以创建一个targetFolder\u输出,如果targetFolder中有一个文件夹,另一个文件夹,我可以在targetFolder\u输出中创建另一个文件夹 最后我得到了C:\targetFolder\anotherFolder,它是重复的C:\targetFolder\u output\anotherFolder\u output 我计划把所有的文件

我正在尝试为在文件夹中的每个文件上运行的批处理脚本创建一个输出文件夹/文件。我希望当我在targetFolder中找到所有文件或文件夹时,我可以创建一个targetFolder\u输出,如果targetFolder中有一个文件夹,另一个文件夹,我可以在targetFolder\u输出中创建另一个文件夹

最后我得到了C:\targetFolder\anotherFolder,它是重复的C:\targetFolder\u output\anotherFolder\u output

我计划把所有的文件都放在那里,但我想如果我能克服这个文件夹障碍,我就能处理一个文件

import os

targetFolder = "C:\\Users\\MyUserName\\Desktop\\tests"
outputFolder = targetFolder + "_output"

# Iterate over everything in the targetFolder
for directory, subdirectories, files in os.walk(targetFolder): 
    folderBasename = (os.path.basename(directory) )

    if not os.path.exists(outputFolder + "\\" + folderBasename + "_output"):
        os.makedirs(outputFolder + "\\" + folderBasename + "_output")
到目前为止,所有这些工作都是在我的桌面上创建一个文件夹“tests_output”,其中包含以下文件夹:
测试\输出\测试\输出
测试\u输出\level2\u输出
测试\u输出\level3\u输出

我想看到的是:
测试\u输出\level2\u输出\level3\u输出

任何帮助都将不胜感激

import os
class FCopy(object):
    def __init__(self, source):
        self.source = source
        self.target = os.path.join(source, '_output')
        if not os.path.exists(self.target):
            os.makedirs(os.path.abspath(self.target))
        self.lf, self.ld = [], []

    def walk(self, path):
        for x in (os.path.join(path, x) for x in os.listdir(path)):
            if x == self.target:continue
            if os.path.isfile(x):
                self.lf.append(x)
            elif os.path.isdir(x):
                self.ld.append(x)
                self.walk(x)
            #extra code if you want to handle symlinks

    def do(self):
        #if thats all you want,
        #code this directly in walk
        self.walk(self.source)

        for x in self.ld:
            x = x.replace(self.source, self.target)
            os.makedirs(x)
        for x in self.lf:
            x = x.replace(self.source, self.target)
            with open(x, 'w') as f:pass

f = FCopy('C:\\PerfLogs\\w\\tmp\\codemirror')
f.do()
你可以玩ifexists等等。 如果您想要的只是源代码的两倍结构,那么您可以在walk中移动文件/目录的创建,在相应的If…else中
注意:忽略我的dir,我只是在其中做了一个测试

使用
os.path.join()
总比手动粘贴路径好。很高兴知道,以后可能会省去我很多头痛。我现在不在电脑旁,但一旦有机会,我一定会检查一下,看看这是否是我需要的!我现在在我的手机里,所以我只能看到这么多,但它看起来很有希望!非常感谢。如果你不想创建文件-我想你不想-在self.lf和from do()之后的两行中对x进行注释/删除抱歉,延迟了很长时间,但我终于要测试代码了。因此,当我设置
FCopy('C:\\Users\\Hobo\\Desktop\\tests')
时,您所做的是在tests文件夹中创建一个文件夹“\u output”,其中包含一个level2文件夹,其中包含一个level3文件夹。我要做的是在桌面上创建一个文件夹“tests_output”,其中的每个文件夹都附加了“_output”。你给我的仍然很有用!我想我可以从这里开始工作,如果没有别的!只要改变目标——这就是我把所有东西都放在一个班里的主要原因。我以为你想要一个文件夹结构的副本,这就是为什么它会创建一个空文件。