Move files in Python

Asked

Viewed 39 times

0

I researched, and I saw people doing like this:

import shutil
shutil.move("este-arquivo", "/tmp")

So I did it here in mine:

import shutil    
shutil.move("arq.txt", "C:\Users\danielly.garcia\Desktop\pasta")

Only, the following error appears:

File "C:/Users/Danielly.Arcia/Desktop/test/Test 01.py", line 7 shutil.move("Arq.txt", "C: Users Danielly.Arcia Desktop folder") Syntaxerror: (error Unicode) 'unicodeescape' codec can’t Decode bytes in position 2-3: truncated UXXXXXXXX escape

I tried with them too:

 import os os.rename("arq.txt",
 "C:\Users\danielly.garcia\Desktop\pasta");

But it makes exactly the same mistake!

  • There is relation to the marked answer but it is something kind of indirect, the problem she is having is precisely because of the U within "C: Users..." that Python is trying to escape as a Unicode character, when in reality it is not actually.

  • The solution to the problem is simple, where there is a backslash you should put two of them, ie where you are C:\Users\danielly.garcia\Desktop\pasta should be 'C: Users Danielly.Arcia Desktop folder' (or if you need a more OS-independent solution use the os.path.join())

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.