我有一个 .csv 文件,如下所示:
name1,name2,name3 and so on
使用 Python 脚本,我试图让它读取 .csv 并为每个值创建目录,例如: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 作为文件夹名称)