cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Python TypeError using Dropbox API move_v2function

Python TypeError using Dropbox API move_v2function

ctubbs
Dropbox Staff

Hi,  I am trying to use the API to batch rename file. I had specific questions are around the move_v2 function:

 

I’m running dropbox.files.move_v2(params), where the args are from_path and to_path – As I understand it, we can’t rename files specifically, rather we just use move/move_v2.

 

However, I keep getting the below error:

 

unnamed.png

 

 

 

 

I know the function exists as a result, but clearly I’m not using it correctly.

11 Replies 11

Greg-DB
Dropbox Staff

Your image unfortunately is not loading. Can you try sharing that again?

ctubbs
Dropbox Staff

unnamed.png

Hi Greg, can you see this?

Greg-DB
Dropbox Staff

Yes, thanks, that's visible now.

 

It looks like you're trying to call the Route object itself, but those aren't the methods for making API calls. To move or rename a file, you should use dropbox.Dropbox.files_move.

 

You can find some basic examples of making Dropbox API calls using the Dropbox Python SDK in this example, such as creating the client here, and making a call to start listing a folder here.

 

To use dropbox.Dropbox.files_move in particular, it would look like:

import dropbox

dbx = dropbox.Dropbox("ACCESS_TOKEN_HERE")

original_path = "/test.txt"
new_path = "/renamed test.txt"
result = dbx.files_move(from_path=original_path, to_path=new_path)

print(result)

LRomano
Explorer | Level 3

Hey Greg,

 

I tried to mod your code with some other snippets I've got so that I can rename all files within a root_file_path

 

file_path = '/root_path_here'

dbx.users_get_current_account()

result = dbx.files_list_folder(file_path, recursive=True)
file_list = []

def process_entries(entries):
    for entry in entries:
        if isinstance(entry, dropbox.files.FileMetadata):
            original = '/'+entry.name
            modified = '/test_'+entry.name
            result = dbx.files_move(from_path=original, to_path=modified)
            print(result)

process_entries(result.entries)


while result.has_more:
    result = dbx.files_list_folder_continue(result.cursor)

    process_entries(result.entries)

But am still ending up with errors. 😞

 

Also, why do we assign the method to var: result and print?

 

Thanks

Luciano

 

Greg-DB
Dropbox Staff

@LRomano What errors are you getting now?

 

Looking at your code, I expect the issue is that you're not specifying the correct file path for the file to move. The Metadata.name that you're using is just the name of the file, not the full path. To get the full path, including any parent path components, you would need to use Metadata.path_lower instead. 

 

Also, I saved the result to a variable and printed it in my sample code just for demonstration purposes. You can do whatever you need in your actual code.

LRomano
Explorer | Level 3

I see, so you need the entire path for each file you iterate over.

 

In which case, if I'm looking to change all names of all files (which have a certain extension, say .mp4 or .png), is there a way to extract just the folder path so I can change the name of each file as a modification of its original?

 

Thanks 🙂

 

Edit:

 

Error was

 

    process_entries(result.entries)

  File "test.py", line 25, in process_entries

    result = dbx.files_move(from_path=original, to_path=modified)

  File "/opt/anaconda3/lib/python3.7/site-packages/dropbox/base.py", line 2398, in files_move

    None,

  File "/opt/anaconda3/lib/python3.7/site-packages/dropbox/dropbox.py", line 338, in request

    user_message_locale)

dropbox.exceptions.ApiError: ApiError('ffb95a528b5ad351d54e7d1722f6baae', RelocationError('from_lookup', LookupError('not_found', None)))

Greg-DB
Dropbox Staff

@LRomano Thanks for sharing the error. That 'from_lookup/not_found' error does indicate that the issue is that the 'from_path' value doesn't point to anything. That does seem to be because you specified just the name and not the path, as discussed.

 

The Metadata.path_lower value is just a string containing the path for the item, which might look something like: "/folder/another folder/file.ext". 

 

Since that's just a string, you can operate on it however you need. For instance, you could split it on "/" and take the parts you want, modify them, and build a new path string. Or, you could use a library or module like pathlib to work with it.

LRomano
Explorer | Level 3

Fantastic. I used rsplit with '/' and to pull the lower_path apart giving me the file's directory and the file name.

 

Then I was able to add some text to the beginning of the file name, concat the whole thing together to create a new_path and it works perfectly!

 

Thanks so much for your help. Just FYI - code below if you want it (I had a spark DF with 1000s of rows of unique data that I needed to add to the beginning of each file):

 

result = dbx.files_list_folder(root_path, recursive=True)

index = 0

def process_entries(entries):
    for entry in entries:

      if isinstance(entry, dropbox.files.FileMetadata):
        path = entry.path_lower.rsplit('/', 1)[0]+'/'
        file = entry.path_lower.rsplit('/', 1)[1]
        path_original = path+file
        global index
        code = sparkDF.collect()[index][2]
        path_new = path+code+'_'+file
        print(path_original)
        print(path_new)
        move = dbx.files_move(from_path=path_original, to_path=path_new)
        index += 1

        
process_entries(result.entries)


while result.has_more:
    result = dbx.files_list_folder_continue(result.cursor)

    process_entries(result.entries)

LRomano
Explorer | Level 3

Hmmmm, actually, it seems to be running ok, but I've had to put in another if statement on has_more = TRUE

 

Even with the while statement, for some reason it was running the function twice. Any idea why?

 

result = dbx.files_list_folder(root_path, recursive=True)

full_list = []

index = 0

def process_entries(entries):
    for entry in entries:

      if result.has_more:
        if isinstance(entry, dropbox.files.FileMetadata):
          file = entry.path_lower.rsplit('/', 1)[1]
          if file.endswith(('.mp4','.png','.mov','.gif','.jpg')):
            path = entry.path_lower.rsplit('/', 1)[0]+'/'
            path_original = path+file
            global index
            code = sparkDF.collect()[index][2]
            file_new = code+'_'+file
            path_new = path+file_new
            sublist = [code,file_new,path_new]
            print(sublist)
            print(result.has_more)
            full_list.append(sublist)
            move = dbx.files_move(from_path=path_original, to_path=path_new)
            index += 1

        
process_entries(result.entries)


while result.has_more:
    result = dbx.files_list_folder_continue(result.cursor)

    process_entries(result.entries)
Need more support?