csciBeginner csciBeginner - 1 year ago 108
Python Question

moving a file in python using shutil

I know there have been some posts on how to move a file in python but I am a little confused. I am working on a program that has a file called test.txt

The file path is this:


I want to move it to:

I tried different variations of what I have below

shutil.move(src, dst)

I keep getting the error no such file in directory.

I was wondering if someone could help me out with the correct way to move the file.

Answer Source

Might be worth checking the file exists and then trying to specify paths using os.path.join:

import shutil
import os
from os.path import join

src = join('/', 'Users', 'username', 'Desktop', 'a.pdf')
dst = join('/', 'Users', 'username', 'Documents', 'a.pdf')

shutil.move(src, dst)

You can first verify if the src actually exists:

>>> True
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download