[英]How to create folder without trailing white space?
In this code I check if a folder exists and if it doesn't I create it, but, I am creating it with a trailing white space because I can't do a + "\\"
with the quotes right in front of the \\
.在此代码中,我检查文件夹是否存在,如果不存在,则创建它,但是,我使用尾随空格创建它,因为我无法在
\\
前面使用引号执行+ "\\"
. How can I add the NewFolderPath
without this space?如何在没有此空间的情况下添加
NewFolderPath
?
dir_carteiras = r"C:\Users\GuilhermeMachado\Documents\Carteiras"
test = os.listdir(dir_carteiras)
for item in test:
if item.endswith(".jpg"):
os.remove( os.path.join(dir_carteiras, item))
today=datetime.date.today()
five_day=datetime.timedelta(days=-1)
d_N1=today+five_day
d_N1_ = d_N1.strftime('%Y.%m.%d')
NewFolderPath = dir_carteiras+"\Historico"+"\ "+d_N1_
isFile = os.path.exists(NewFolderPath)
You're looking for os.path.join()
.您正在寻找
os.path.join()
。 It inserts the correct symbol for you.它会为您插入正确的符号。
NewFolderPath = os.path.join(dir_carteiras,"Historico",d_N1_)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.