Python 如何复制文件夹的名称

Python 如何复制文件夹的名称,python,if-statement,copy,directory,names,Python,If Statement,Copy,Directory,Names,我是python新手,我正在努力完成一项简单的任务。我试图只复制一些文件夹的名称到另一个文件夹(例如:folderA)。我不在乎那些文件夹的内容,我只在乎复制它们的名字 (我要复制的文件夹名称在批次_2016下:sin_1008100、sin_1010987、sin_10109) 下面是我写的东西,但并不像我预期的那样工作 batch_path = '/net/storage/batch_2016' # where the folders are located batch_name = raw

我是python新手,我正在努力完成一项简单的任务。我试图只复制一些文件夹的名称到另一个文件夹(例如:folderA)。我不在乎那些文件夹的内容,我只在乎复制它们的名字

(我要复制的文件夹名称在批次_2016下:sin_1008100、sin_1010987、sin_10109) 下面是我写的东西,但并不像我预期的那样工作

batch_path = '/net/storage/batch_2016' # where the folders are located
batch_name = raw_input("batch name: ") # im giving a new folder name
os.chdir(batch_path)
print(os.getcwd())

for fName in os.listdir('.'):
    if fName.startswith("sin"):
        os.makedirs(batch_name)
        os.chdir(batch_name)
        os.makedirs(fName)
我没有收到任何错误,但当它运行时,它会创建3个批处理名称文件夹,每个文件夹都有我要复制的文件夹名称。 因此,如果新文件夹名为FolderA,则其内部

FolderA, sin_1008100
FolderA, sin_1010987
FolderA, sin_10109
我想这是因为循环,但我不知道如何修复它。
非常感谢您的帮助。

我想这正是您想要的:

import os

batch_path = '/net/storage/batch_2016' # where the folders are located
batch_name = raw_input("batch name: ") # im giving a new folder name
os.chdir(batch_path)
print(os.getcwd())

os.makedirs(batch_name)

for fName in os.listdir('.'):
    if fName.startswith("sin"):
        os.makedirs(batch_name + "/" + fName)
        # alternatively, os.makedirs("../" + batch_name + "/" + fName)

是的,它起作用了!这就是我想要的,谢谢。顺便问一下,什么是“/”?“/”是路径的一部分-您将在目录batch_name中创建一个名为fName的文件。例如,使用batch_name=“/net/storage/greatbatch”和fName=“sin_42”,可以得到/net/storage/greatbatch/sin_42。