我有一個.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