创建文件夹层次结构
Create Folder Hierachy
我目前正在学习 Python 的基础知识。目前,我正在尝试编写一个脚本,该脚本从文本文件列表创建文件夹(第一个层次结构),每个文件夹都具有相同的子文件夹结构,可以下降到 3 个层次结构。
我已经完成了第一部分:从文本文件创建第一层级文件夹。
from sys import argv
import os;
home_dir = '/home/tobi/Python'
mylist = open('test.txt','r')
for folder in mylist:
folder = folder.strip()
newFolder = home_dir+'/'+folder
print "Folder name " +newFolder
if not os.path.exists(newFolder):
os.makedirs(newFolder)
os.chdir(newFolder)
mylist.close()
现在我正在努力创建一个循环以将我给定的子文件夹结构插入到每个已创建的文件夹中。
我该怎么做?
你真的应该休息一下,理清思绪,然后重写你的问题,例如你的问题是
Create Folder Hierachy from Text files
然后你写
I've managed to do the first part: Create the 1st hierarchy folders from a text file.
您还对子目录的结构自相矛盾……但这里有一个平面层次结构的猜测可以让您开始 "loop problem"。
请注意,我使用的是 Python 3,因此您可能需要调整一两件事。另外,我实际上并没有创建目录(所以这对你来说可能会失败,你可能必须加入路径):
import os
home_dir = "."#'/home/tobi/Python'
# don't have your subdirectory structure, making something up ...
subdirs = [ "{0:02}_Subfolder{0}".format(n) for n in range(1,8+1)]
#with open('test.txt','r') as mylist: # don't have your file, making something up ...
mylist = ["one", "two", "three"]
# indent for with
for folder in mylist:
folder = folder.strip()
newFolder = home_dir + '/' + folder
print("Folder name " + newFolder)
if not os.path.exists(newFolder):
#os.makedirs(newFolder)
print("mkdir:", newFolder) # just pretending
#os.chdir(newFolder)
print("cwd:", newFolder) # just pretending
# just another loop like the one you already came up with (did you really?)
for subdir in subdirs:
if not os.path.exists(subdir):
#os.makedirs(subdir)
print("mkdir:", subdir) # just pretending
# done
给予
Folder name ./one
mkdir: ./one
cwd: ./one
mkdir: 01_Subfolder1
mkdir: 02_Subfolder2
mkdir: 03_Subfolder3
mkdir: 04_Subfolder4
mkdir: 05_Subfolder5
mkdir: 06_Subfolder6
mkdir: 07_Subfolder7
mkdir: 08_Subfolder8
Folder name ./two
mkdir: ./two
cwd: ./two
mkdir: 01_Subfolder1
mkdir: 02_Subfolder2
mkdir: 03_Subfolder3
mkdir: 04_Subfolder4
mkdir: 05_Subfolder5
mkdir: 06_Subfolder6
mkdir: 07_Subfolder7
mkdir: 08_Subfolder8
Folder name ./three
mkdir: ./three
cwd: ./three
mkdir: 01_Subfolder1
mkdir: 02_Subfolder2
mkdir: 03_Subfolder3
mkdir: 04_Subfolder4
mkdir: 05_Subfolder5
mkdir: 06_Subfolder6
mkdir: 07_Subfolder7
mkdir: 08_Subfolder8
感谢您的回复!问题解决了!致所有回复者: 抱歉我的问题描述不准确。我想正确的表述应该是:将子文件夹结构插入给定目录中的每个文件夹。我也猜想这更多是我缺乏基本知识而不是问题。在发布下一个问题之前,我会深入了解基础知识!
我目前正在学习 Python 的基础知识。目前,我正在尝试编写一个脚本,该脚本从文本文件列表创建文件夹(第一个层次结构),每个文件夹都具有相同的子文件夹结构,可以下降到 3 个层次结构。
我已经完成了第一部分:从文本文件创建第一层级文件夹。
from sys import argv
import os;
home_dir = '/home/tobi/Python'
mylist = open('test.txt','r')
for folder in mylist:
folder = folder.strip()
newFolder = home_dir+'/'+folder
print "Folder name " +newFolder
if not os.path.exists(newFolder):
os.makedirs(newFolder)
os.chdir(newFolder)
mylist.close()
现在我正在努力创建一个循环以将我给定的子文件夹结构插入到每个已创建的文件夹中。
我该怎么做?
你真的应该休息一下,理清思绪,然后重写你的问题,例如你的问题是
Create Folder Hierachy from Text files
然后你写
I've managed to do the first part: Create the 1st hierarchy folders from a text file.
您还对子目录的结构自相矛盾……但这里有一个平面层次结构的猜测可以让您开始 "loop problem"。 请注意,我使用的是 Python 3,因此您可能需要调整一两件事。另外,我实际上并没有创建目录(所以这对你来说可能会失败,你可能必须加入路径):
import os
home_dir = "."#'/home/tobi/Python'
# don't have your subdirectory structure, making something up ...
subdirs = [ "{0:02}_Subfolder{0}".format(n) for n in range(1,8+1)]
#with open('test.txt','r') as mylist: # don't have your file, making something up ...
mylist = ["one", "two", "three"]
# indent for with
for folder in mylist:
folder = folder.strip()
newFolder = home_dir + '/' + folder
print("Folder name " + newFolder)
if not os.path.exists(newFolder):
#os.makedirs(newFolder)
print("mkdir:", newFolder) # just pretending
#os.chdir(newFolder)
print("cwd:", newFolder) # just pretending
# just another loop like the one you already came up with (did you really?)
for subdir in subdirs:
if not os.path.exists(subdir):
#os.makedirs(subdir)
print("mkdir:", subdir) # just pretending
# done
给予
Folder name ./one
mkdir: ./one
cwd: ./one
mkdir: 01_Subfolder1
mkdir: 02_Subfolder2
mkdir: 03_Subfolder3
mkdir: 04_Subfolder4
mkdir: 05_Subfolder5
mkdir: 06_Subfolder6
mkdir: 07_Subfolder7
mkdir: 08_Subfolder8
Folder name ./two
mkdir: ./two
cwd: ./two
mkdir: 01_Subfolder1
mkdir: 02_Subfolder2
mkdir: 03_Subfolder3
mkdir: 04_Subfolder4
mkdir: 05_Subfolder5
mkdir: 06_Subfolder6
mkdir: 07_Subfolder7
mkdir: 08_Subfolder8
Folder name ./three
mkdir: ./three
cwd: ./three
mkdir: 01_Subfolder1
mkdir: 02_Subfolder2
mkdir: 03_Subfolder3
mkdir: 04_Subfolder4
mkdir: 05_Subfolder5
mkdir: 06_Subfolder6
mkdir: 07_Subfolder7
mkdir: 08_Subfolder8
感谢您的回复!问题解决了!致所有回复者: 抱歉我的问题描述不准确。我想正确的表述应该是:将子文件夹结构插入给定目录中的每个文件夹。我也猜想这更多是我缺乏基本知识而不是问题。在发布下一个问题之前,我会深入了解基础知识!