Aneesha Aggarwal
Aneesha Aggarwal

Reputation: 23

Moving files with python shutil library

I am trying to move specific folders within a file directory inside a flash drive with the Python shutil library. I am getting the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'D:\\New Folder\\CN00020'. 

I have looked at some questions posted and I think my issue may be that I am not declaring the filepath correctly. I am using the Spyder app for Python and Windows 10.

import shutil
shutil.move('D:\\New Folder\CN00020', 'D:\\Batch Upload')

Upvotes: 2

Views: 204

Answers (1)

Justus
Justus

Reputation: 56

the problem is that \ has special meaning. Python interprets \C as a special character. There are three solutions:

# escape backspace
shutil.move('D:\\New Folder\\CN00020', 'D:\\Batch Upload')

# use raw strings
shutil.move(r'D:\New Folder\CN00020', r'D:\Batch Upload')

# use forward slashes which shutil happens to support
shutil.move('D:/New Folder/CN00020', 'D:/Batch Upload')

Upvotes: 4

Related Questions