Reputation: 39
I have the following problem. I want to write a script for automatic folder structure creation defined inside a txt. However the following code creates strange multiple directories where one directory end with ?
[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?
An extract from the txt file used to create the folders:
.
./03_rig
./03_rig/release
./03_rig/working
./05_shading
./05_shading/release
./05_shading/working
Here is the code:
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")
Update:
The default text editor that comes with linux doesn't show any whitespace at the end of the line, while the nano editor allows me to jump into a sort of whitespace like position (I haven't used cli editors tho, so I don't know if it is a feature to do that or if the character really exists) WHY the hell wouldn't the default editor not show these characters? That could be so dangerous! By the way the txt was created with the command find . -type d > output.txt
which makes it even stranger that linux does this. Any explanation?
Upvotes: 1
Views: 145
Reputation: 2919
I bet the character isn't a question mark, that's just ls
's way of telling you that there's an unprintable character. The unprintable character in question is a carriage return character (CR
, commonly represented as \r
or \015
or ^M
).
Unix uses the LF
character (\n
, \012
, ^J
) as a line terminator (i.e. every line consists of its printable characters followed by LF).
Update:
Alright, I found where you're erring, its the name of the txt file. You see after every name on the of the file, you have a trailing white-space, which is possibly causing the error since you cannot have a trailing white-space at the end of a folder name. Remove the white-spaces at end of every name in the txt file, and see it work, or even better, use the strip()
string function where you're reading the names, eg. path.strip()
. ;)
Upvotes: 1