我有一个.csv文件,如下面的:使用Python脚本我试图把它读.csv和创建目录为每个值 如从CSV使用数据创建目录
name1,name2,name3 and so on
: name1,name2,name3
将创建这些目录:name1 and name2 and name3
这是到目前为止我的代码:
import os
import fileinput
textFile = 'E:/Videos/Movies/subtest/dirlist.csv'
path = "E:/Videos/Movies/subtest/"
#generate a txt file with the current names of the directories
def makeFile():
# Open a file
dirs = os.listdir(path)
# This would print all the files and directories
for file in dirs:
#open the file
tFO = open(textFile, "ab+")
#write to the file, seprating each item with "||"
tFO.write(file + ',')
#print output
print (file)
#prints confirmation
print 'file printed!'
#close the file
tFO.close()
mainMenu()
def makeDirs():
#open textFile as read only and set its varible as myListRead
myListRead = open(textFile, 'rb+')
#reads the x amount of lines and stores it as str
str = myListRead.read();
for line in str:
os.makedirs(path + str)
print 'directories created:', str
运行该代码创建作为我打算将.csv,但当我运行makeDirs()它使所有的.csv的目录名(1,名称2,名称3作为文件夹名)
你能澄清你的代码有什么问题吗? – Alex
你的问题到底是什么? – BlackVegetable
你能打印出你的os.makedirs的路径+ str,并将它张贴在这里吗? – sihrc