简体   繁体   English

如何复制文件夹的名称

[英]how to copy the name of the folders

I am new to python and I am really trying to accomplish a simple task. 我是python的新手,我真的在尝试完成一个简单的任务。 I am trying to copy only the names of some folders to another folder (ex: folderA). 我正在尝试仅将某些文件夹的名称复制到另一个文件夹(例如:folderA)。 I do not care about the contents of those folders, I do care to copy only their names. 我不在乎这些文件夹的内容,我只是在复制它们的名称。

(the folder names I want to copy are under batch_2016: sin_1008100, sin_1010987, sin_10109) below is what I have written but is not working as I expect (我要复制的文件夹名称位于batch_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)

I am not getting any error to post but when it is running it creates 3 batch_name folders and each folder has the folder_names I want to copy. 我没有发布任何错误,但运行时会创建3个batch_name文件夹,每个文件夹都有我要复制的folder_names。 So if the new folder name is FolderA, it has inside 因此,如果新的文件夹名称为FolderA,则其中包含

FolderA, sin_1008100
FolderA, sin_1010987
FolderA, sin_10109

I guess this is because of the loop but I am not sure how to fix that. 我猜这是由于循环造成的,但我不确定如何解决该问题。 Any help would be appreciated, thank you. 任何帮助,将不胜感激,谢谢。

I think this does what you want: 我认为这可以满足您的需求:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM