0

I am trying to implement a dropbox app which downloads files from user's Dropbox account.While creating destination path in user's local directory,it crashes saying

Error occured [400] {u'path : u''invalid path /New folder\\img1.jpg : character at index 11 backslashes not allowed}

I thought the dropbox's folder hierarchy uses forward slashes to represent nesting of dorectories, and windows uses backward slashes, so they might be conflicting. Then I used python's BIF replace() as follows for different paths

sample_path.replace( "\\", "/" )

but still

complete_path

variable in my code is giving path containing backslash,after which the program crashes. the folder hierarchy in my dropbox account is :

New Folder :
            Img1.jpg
dtu.jpg
img.jpg

the code is :

def download_file(self,source_path,target_path):
    print 'Downloading %s' % source_path
    file_path = os.path.expanduser(target_path)
    (dir_path,tail) = os.path.split(target_path)
    self.check_dir(dir_path)
    to_file = open(file_path,"wb")
    print source_path+"!!!!!!!!!!!!!!!!!!!!!!!!!!"
    source_path.replace("\\","/")        
    f= self.mClient.get_file(source_path) #  request to server !
    to_file.write(f.read())
    return
def download_folder(self, folderPath):


    # try to download 5 times to handle http 5xx errors from dropbox

    try:
        response = self.mClient.metadata(folderPath)
            # also ensure that response includes content
        if 'contents' in response:
            for f in response['contents']:
                name = os.path.basename(f['path'])
                complete_path = os.path.join(folderPath, name)
                if f['is_dir']:            
                    # do recursion to also download this folder
                    self.download_folder(complete_path)
                else:
                    # download the file
                    self.download_file(complete_path, os.path.join(self._target_folder, complete_path))
        else:
            raise ValueError
    except (rest.ErrorResponse, rest.RESTSocketError, ValueError) as error:
            print 'An error occured while listing a directory. Will try again in some seconds.'
            print "Error occured "+ str(error)
vertexion
  • 536
  • 2
  • 7
  • 20
  • how are you calling those functions? and you're running on windows? – Loïc Faure-Lacroix Dec 21 '13 at 07:18
  • function call links as - main() -> init_download() -> download_folder(), yeah I am running windows – vertexion Dec 21 '13 at 07:20
  • btw, putting replace everywhere isn't going to help. Using the path module will work without replacing anything. Replace paths only before sending to the server. – Loïc Faure-Lacroix Dec 21 '13 at 07:26
  • @Loïc Faure-Lacroix I have updated the code. Now I am using "replace()" only before call to the server.As I observed, "source_path" before using replace() is : New Folder\img1.jpg and After using replace("\\","/") it remains the same ! I have tried same function on the strings which works, but this isn't! – vertexion Dec 21 '13 at 08:12
  • what does it print in the console now? does it have "\\" in it or the error message changed? – Loïc Faure-Lacroix Dec 21 '13 at 10:58
  • source_path still has '\\' in it. – vertexion Dec 21 '13 at 11:40
  • If you want to replace all occurrences in a string, use a count parameter in the third position: source_path = source_path.replace("\\", "/", 10) – donarb Dec 21 '13 at 15:36

1 Answers1

3

Try this in the Python console to see the issue:

>>> x = "hello"
>>> x.replace("hello", "goodbye")
'goodbye'
>>> x
'hello'

Calling replace on a string doesn't actually modify the string. It returns a new string with the replacement. So you probably want to do this instead:

source_path = source_path.replace("\\", "/")
user94559
  • 59,196
  • 6
  • 103
  • 103