gpt4 book ai didi

Python 产生奇怪的多个目录

转载 作者:行者123 更新时间:2023-12-01 08:51:00 25 4
gpt4 key购买 nike

我遇到以下问题。我想编写一个脚本来自动创建在 txt 中定义的文件夹结构。然而,以下代码创建了奇怪的多个目录,其中一个目录以 ?

结尾
[centos7@localhost table01]$ ls
01_lay 02_model 03_rig 04_texture 05_shading references?
01_lay? 02_model? 03_rig? 04_texture? 05_shading?

用于创建文件夹的 txt 文件的摘录:

.
./03_rig
./03_rig/release
./03_rig/working
./05_shading
./05_shading/release
./05_shading/working

这是代码:

import os

#Global Variables
pathToProject = "/run/media/centos7/Data/Programming/Pipeline/SampleProject"
pathToPipeline = "/run/media/centos7/Data/Programming/Pipeline/Pipeline"

allowedTypes = ["asset_character", "asset_prop", "asset_dmp", "scene", "shot"]

def createFolders(type, name):
if type in allowedTypes:
if type == "asset_character":
pass
elif type == "asset_prop":
pathToCreateInside = os.path.join(pathToProject, "03_assets/prop/"+name)
os.mkdir(pathToCreateInside)
pathToTxt = os.path.join(pathToPipeline, "create_folders/folder_setups/asset_cg.txt")
if os.path.isdir(pathToCreateInside) == True and os.path.isfile(pathToTxt) == True:
makeDirStructure(pathToCreateInside, pathToTxt)
else:
print pathToTxt
print pathToCreateInside
print "Error: Folder or file does not exist "
elif type == "asset_dmp":
pass
elif type == "scene":
pass
elif type == "shot":
pass

else:
print "Only the following foldertypes are accepeted: ", allowedTypes

def makeDirStructure(pathToCreateInside, pathToTxt):
with open(pathToTxt) as f:
next(f)
for path in f:
try:
os.makedirs(os.path.join(pathToCreateInside, path[2:]))
except OSError:
print ("Creation of the directory %s failed" % path)

createFolders("asset_prop","table01")

更新:Linux 附带的默认文本编辑器不会在行尾显示任何空格,而 Nano 编辑器允许我跳到类似空格的位置(我还没有使用过 cli 编辑器,所以我不这样做)不知道这是否是一个功能,或者角色是否真的存在)为什么默认编辑器不显示这些角色?那可能会非常危险!顺便说一句,该 txt 是使用命令 find 创建的。 -type d > output.txt 这使得 linux 这样做更加奇怪。有什么解释吗?

最佳答案

我敢打赌这个字符不是问号,这只是 ls 告诉您有一个无法打印的字符的方式。所讨论的不可打印字符是回车符(CR,通常表示为 \r\015^M).

Unix 使用 LF 字符(\n\012^J)作为行终止符(即每一行都包含其可打印字符,后跟 LF)。

更新:

好吧,我找到你错的地方了,就是txt文件的名字。您会看到文件的每个名称后面都有一个尾随空格,这可能会导致错误,因为文件夹名称末尾不能有尾随空格。删除 txt 文件中每个名称末尾的空格,然后查看它是否有效,或者更好的是,在读取名称时使用 strip() 字符串函数,例如。 path.strip();)

关于Python 产生奇怪的多个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53126755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com