简体   繁体   English

如何创建没有尾随空格的文件夹?

[英]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.

相关问题 在python 3中删除尾随空白 - Remove trailing white space in python 3 如何在Python中不留空格的情况下打印 - How to print without trailing space in Python 熊猫修剪数据框中的前导和尾随空格 - Pandas trim leading & trailing white space in a dataframe 在Sublime文字3上按需触发尾随空白 - Trigger trailing white space on demand on Sublime text 3 基于空白和尾随标点符号化? - Tokenize based on white space and trailing punctuation? 如何使用Selenium和Python在innerText包含前导和尾随空格字符时定位和单击元素 - How to locate and click the element when the innerText contains leading and trailing white-space characters using Selenium and Python 当 textContext 包含前导和尾随空白字符时如何单击按钮? - How to click on the button when the textContext contains leading and trailing white-space characters? 如何在没有可见空白的情况下扩展/减少 QTable 视图 - how to expand /decrease QTable View without visible blank white space 如何删除python中的尾随空白? - How to remove the trailing white spaces in python? 正则表达式:如何捕获可能由空格分隔的 6-12 位数字序列而不捕获任何尾随空格 - Regex: How to capture a sequence of 6-12 digits that may be separated by spaces without capturing any trailing space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM