【发布时间】:2021-01-08 13:53:52
【问题描述】:
挑战是从文本文件中获取名称并在不同的目录中创建一个新文件夹。我的问题是,当我尝试这样做时,它提到了
join() argument must be str, bytes, or os.PathLike object, not 'list'
有没有一种方法可以将列表转换为执行此操作,或者是否有其他方法可以执行此操作?
import os
clientnames = "/home/michael/tafe/Customer Service Team/Customer Service Team new client names" #filepath/filename of new client names.
#Error handeling, if the txt file is missing then it will say the file is unavaliable.
if (os.path.isfile(clientnames)) == True:
print("The file is avaliable, folder creation will now start.")
else:
print("The file is unavaliable, please provide Customer Service Team new client names .txt file")
folderdir = "/home/michael/tafe/FS1/Administration/New_Customers" #filepath of where the new folders are to be created in.
#take name from txt file.
with open(clientnames, "r") as newfolder:
for line in newfolder:
newfolder = line.strip().split()
created_folder = os.path.join(folderdir, newfolder)
os.mkdir(created_folder)
print("Directory '% s' created" % newfolder)
我对学习 python 还很陌生,但是一旦我找到了如何创建目录,我感觉我已经接近解决这个问题了。 (挑战还有其他一些部分,但与此无关......)。
在 Visual Studio Code 上使用 Python 3.8.7 64 位。
【问题讨论】:
-
好吧,
newfolder是一个列表,所以不清楚您要达到的目标是什么,但不妨试试os.path.join(folderdir, *newfolder)。注意星号。
标签: python python-3.x text directory