[英]Python - replace backslash with forward slash (Dropbox aspect)
I have a problem, which I can't deal with.我有一个问题,我无法处理。 I'm trying to make my own program to send files to Dropbox, but their system doesn't allow backslashes.
我正在尝试制作自己的程序以将文件发送到 Dropbox,但他们的系统不允许使用反斜杠。
When I want to send path to file like this:当我想像这样发送文件路径时:
..\\Users\\TS\\Desktop\\Program\\Nowy dokument tekstowy - Kopia (2).txt
I got error:我有错误:
ErrorResponse: [400] {u'path': u"Invalid path '/..\\\\Users\\\\TS\\\\Desktop\\\\Program\\\\Nowy dokument tekstowy - Kopia (2).txt': character at index 3: backslash not allowed"}
I googled for this (ie here and here ), searched in Python's os.path docs but it didn't help me.我用谷歌搜索了这个(即这里和这里),在 Python 的 os.path 文档中搜索,但它没有帮助我。
I mean output for this code:我的意思是这段代码的输出:
s = r'..\\Users\\TS\\Desktop\\Program\\Nowy dokument tekstowy - Kopia (2).txt ' s.replace('\\\\', '/') print s
or this:或这个:
s = r'..\\Users\\TS\\Desktop\\Program\\Nowy dokument tekstowy - Kopia (2).txt ' s.replace('\\\\', '/') print s
is the same:是一样的:
..\\Users\\TS\\Desktop\\Program\\Nowy dokument tekstowy - Kopia (2).txt
I need to deal with relatives paths, not absolutes.我需要处理相对路径,而不是绝对路径。 Any other idea how to deal with paths like this?
任何其他想法如何处理这样的路径? Or maybe how to make Dropbox accepts backslashes?
或者如何让 Dropbox 接受反斜杠?
EDIT: I'm using Python2.7编辑:我使用的是 Python2.7
s = r'..\Users\TS\Desktop\Program\Nowy dokument tekstowy - Kopia (2).txt '
s = s.replace('\\', '/')
print s
Output: ../Users/TS/Desktop/Program/Nowy dokument tekstowy - Kopia (2).txt
输出:
../Users/TS/Desktop/Program/Nowy dokument tekstowy - Kopia (2).txt
/ ../Users/TS/Desktop/Program/Nowy dokument tekstowy - Kopia (2).txt
/ ../Users/TS/Desktop/Program/Nowy dokument tekstowy - Kopia (2).txt
Note that s.replace()
by itself doesn't do anything.请注意,
s.replace()
本身不做任何事情。 You have to assign s = s.replace()
to have the desired effect.您必须分配
s = s.replace()
才能获得所需的效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.