Python 如何创建没有尾随空白的文件夹?
在这段代码中,我检查文件夹是否存在,如果不存在,我将创建它,但是,我创建它时使用了一个尾随空格,因为我不能在\前面加引号。如果没有此空间,如何添加NewFolderPathPython 如何创建没有尾随空白的文件夹?,python,path,operating-system,Python,Path,Operating System,在这段代码中,我检查文件夹是否存在,如果不存在,我将创建它,但是,我创建它时使用了一个尾随空格,因为我不能在\前面加引号。如果没有此空间,如何添加NewFolderPath dir_carteiras = r"C:\Users\GuilhermeMachado\Documents\Carteiras" test = os.listdir(dir_carteiras) for item in test: if item.endswith(".jpg"): os.remov
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)
您正在寻找os.path.join。它为您插入正确的符号
NewFolderPath = os.path.join(dir_carteiras,"Historico",d_N1_)
\\是在Python中表示单个反斜杠\的一种方法,不带尾随空格字符。请清楚所需的结果。下面的答案不符合我的想法。反斜杠在字符串中的各种用法都有很好的文档记录,因此我特别不确定在哪里您很难得到想要的结果。