python删除目录名两端/
大家讲道理
大家讲道理 2017-04-18 10:19:41
0
3
591

有一个字符串,比如

/home/demo

我想补全这个字符串的斜线,变成

/home/demo/

然后再进行拼接字符串的操作
于是打算先删除两端的'/'(如果有的话),

home/demo

再在两端添加'/',

/home/demo/

该怎么做比较简单,求赐教

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
小葫芦

If it were just 目录名,两遍直接加斜线,对于unix系统来说,并不会改变打开效果。
理论上windows类似,使劲加/,系统也会自动简化成一个。对,我说的是c:/Users/aa, not a backslash...

for i in os.walk("/tmp/a")
    print i
    
for i in os.walk("/tmp/a/")
    print i

for i in os.walk("//tmp/a")
    print i
    
for i in os.walk("//tmp///a//")
    print i    

However, we should pay attention to two problems here. Both of these problems belong to improper use of changing the original path:

If it is originally a relative path, then after adding /之后会变成绝对路径,有可能会改变你本来想打开的路径。
如果原来是文件,那么后面加了/ in front, it will become a path, and an error will occur when opening.

About default permissions:

It is recommended to throw the following functions into ~/.bashrc

toucha(){
    touch 
    chmod +x 
}

This way the shell can use the toucha command directly

Ty80

General programming languages ​​will have a trim function, which can delete specified characters on both sides of a string. Generally, there are also ltrim and rtrim, which are used to only delete specified characters on the left or right side. Although python is not named after trim, you search for "python trim" Still using keywords, you can still find the answer you want. Good luck

阿神

One of the methods

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template