简体   繁体   中英

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.

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.

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?

EDIT: I'm using 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

Note that s.replace() by itself doesn't do anything. You have to assign s = s.replace() to have the desired effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM