Yashwanth Nataraj
Yashwanth Nataraj

Reputation: 193

how to copy files from once location to another without using shutil in python

I tried with shutil but python debugger is throwing error..can i know why is this?? and is there any other way??

path = "C:\\Program Files (x86)"
if os.path.exists(path):
    src= "C:\\Program Files (x86)\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
else:
    src= "C:\\Program Files\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
dest = "C:\\Sanity_Automation\\"
shutil.copy(src,dest)

Update:

Getting this error :

Traceback (most recent call last):
     File "C:\Sanity_Automation\Work_Project\copy.py", line 15, in <module>
shutil.copy(src, dest)
     File "C:\Sanity_Automation\Python272\lib\shutil.py", line 116, in copy
copyfile(src, dst)
     File "C:\Sanity_Automation\Python272\lib\shutil.py", line 81, in copyfile
with open(src, 'rb') as fsrc:
     IOError: [Errno 13] Permission denied: 'C:\\Program Files (x86)\\Agilent\\N2X\\QuickTest\\Scripts\\R

Upvotes: 2

Views: 6858

Answers (2)

Torxed
Torxed

Reputation: 23480

import os
os.system('mv /src/path /new/path')

or

import os
os.rename('/source/path', '/source/new_path')

There's your only two options besides shutils (but both are not a solution to your problem)

Secondly, your original problem is most likely because you're running Win7 (or a 64 bit server OS) and you don't run your cmd.exe promt (or python script) as administrator.
Administrating C:\Program Files\ (and (x86)) is forbidden for normal users.

Try placing your scripts in C:\Users\<your username>\Desktop\quicktest\ instead and see if you get the same error. or run the cmd.exe or python script as administrator.

Upvotes: 1

icecrime
icecrime

Reputation: 76745

Using shutil is the proper way to achieve what you want, so you should probably understand why it fails rather than search for an alternative.

You traceback shows:

IOError: [Errno 13] Permission denied: 'C:\\Program Files (x86)\\Agilent\\N2X\\QuickTest\\Scripts\\R

Using an alternative copying method won't be able to fix a permission issue. As stated by Torxed in his answer, you are most likely running under Windows Seven where the Program Files directory is under restrictive permissions.

On a side note, you should consider using raw strings for paths containing backslashes by prefixing literals with an r:

path = r"C:\Program Files (x86)"

Upvotes: 2

Related Questions