N
N
NakedFace2016-03-12 21:49:06
Python
NakedFace, 2016-03-12 21:49:06

How to move a file using Python?

I tried using the os library:

os.rename('D:\\OneDrive\\Python\\PYTHON_LECTURES\\lecture3.py', 'D:\\OneDrive\\Python\\PYTHON_LECTURES\\small\\lecture3.py')

Gives an error: WindowsError: [Error 3]
Tried with shutil:
shutil.copyfile(r'D:\OneDrive\Python\PYTHON_LECTURES\test.py', r'D:\OneDrive\Python\PYTHON_LECTURES\test\test.py')

Error: IOError: [Errno 2] No such file or directory: 'D:\\OneDrive\\Python\\PYTHON_LECTURES\\test\\test.py'
Escaped slashes, didn't escape slashes. I can't figure out what the problem is.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2016-03-12
@NakedFace

the problem is in

dirname = os.path.split(filename)[0]
        if not os.path.exists(dirname):
            os.makedirs(dirname)

Z
Zhbert, 2016-03-12
@Zhbert

Something like this

D
DmitriyL02, 2017-04-05
@DmitriyL02

Shutil Library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question