Reputation: 6657
I want to shutil.copy()
function to copy file to another directory. I try to execute the following code:
copy(open("/home/dizpers/pytest/testfile1.txt", "r"), "/home/dizpers/pytest")
But python shell shows me the error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/shutil.py", line 116, in copy
dst = os.path.join(dst, os.path.basename(src))
File "/usr/lib/python2.7/posixpath.py", line 112, in basename
i = p.rfind('/') + 1
AttributeError: 'file' object has no attribute 'rfind'
So, I understand why this problem appears. I open the file with open()
function. And I think that I also should open a directory like this. How can I do this?
Thanks in Advance!
Upvotes: 4
Views: 3888
Reputation: 56390
shutil.copy
takes in two paths, not a file object and a path, you should just specify the path instead of creating a file object for the first argument
You can use shutil.copyfileobj
if you need to use a file object for the first argument, but you'll have to use a file object for the second argument as well.
Upvotes: 5