【发布时间】:2022-01-17 04:00:38
【问题描述】:
第一次在这里提问,如果我做的不对,请随时告诉我。
我实际上是在尝试将 txt 文件从字典(值)附加到具有 os 和路径的特定目录。我试过这个,但我得到一个类型错误。 TypeError: join() argument must be str, bytes, or os.PathLike object, not 'list'
下面是我的代码。我期待您的回复:)
import os
ROOT = os.getcwd()
DATA_ROOT = os.path.join(ROOT, "src/data/initial")
def classify(arg: dict):
for key, value in arg.items():
folder = os.path.join(DATA_ROOT, key)
file = os.path.join(folder, value)
os.rename(DATA_ROOT, file)
return
# Dictionary with files that I am trying to move/create in the path
# .....src/data/initial/personal and .....src/data/initial/work
categories = {
"personal": ["todos.txt", "bookmarks.txt"],
"work": ["customers.csv", "jobs.csv"]
}
print(classify(categories))
【问题讨论】:
-
file = os.path.join(folder, value) # value 是类别的值,所以它是一个列表——这不是 os.path.join 所期望的
标签: python join path operating-system rename