1

i've been working on a program for a couple of days now, and i'm stuck on a error since last night. I've already looked over other questions here but none seem to work. I'm moving files from 1 drive to another using shutil, and in the beginning it worked, but now it starts saying this error :

EDIT: Just noticed that it does in fact move the files, and after that it gives me the error

Traceback (most recent call last):
  File "C:\Users\DC\Desktop\Prototype\Prototype", line 9, in <module>
    shutil.move(src, dst)
  File "C:\Python27\lib\shutil.py", line 300, in move
    rmtree(src)
  File "C:\Python27\lib\shutil.py", line 256, in rmtree
    onerror(os.rmdir, path, sys.exc_info())
  File "C:\Python27\lib\shutil.py", line 254, in rmtree
    os.rmdir(path)
WindowsError: [Error 5] Access is denied: 'E:'

And here's my code snippet.

import os
import time

time = time.strftime("%H%M%S")
src = "E:"
dst = "C:\Users\DC\Desktop\Data_" + str(time)

import shutil
shutil.move(src, dst)
print 'Done'

2 Answers2

1

Are you trying to move the files from E: or the drive itself? :-)

Check if E: drive is empty, if it is it means that there's nothing to move. If you want to move the files, not the drive, use a wildcard.

hint: E:\*.*

UPDATE:

Linu 82 of shutil.py in the exception you quoted seems to do simple open syscall:

with open(src, 'rb') as fsrc:

It seems it cannot handle windows drives or patterns like e:\\*.*:

fo = open(r'D:\*.*','rb')

D:\>python testopen.py
Traceback (most recent call last):
  File "testopen.py", line 2, in <module>
    fo = open(r'D:\*.*','rb')
IOError: [Errno 22] invalid mode ('rb') or filename: 'D:\\*.*'

However, for you a very simple workaround would suffice: os.listdir.

>>> import os
>>> os.listdir('D:\\')
['Videos', 'Pictures', 'Music', 'Documents', ...]

os.listdir returns a list of files and dirs in a particular dir or windows drive. Once you have a list, do shutil.move of each item.

LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93
  • I am moving from, and there are files, and manually i can just move everything in there.. Thats why i dont really get it – user2340383 Jul 25 '14 at 11:07
  • @user2340383 Try using the wildcard `*` after the drivename as per John's answer. – TheDarkTurtle Jul 25 '14 at 11:12
  • I have no access to such Windows machine, so I can only speculate: shutil.move(src) tries to move 'E:' path because that's what you have given it. If there were files in it, it could have moved the files. But now that's empty, it tries to move 'E:' and obviously it can't move drive itself. hence the error. Try smth like "E:\\*.*' – LetMeSOThat4U Jul 25 '14 at 11:13
  • To be clear, i wanna move the files from E, but i think it moves the files, and then tries to move to drive itself, but that obviously wont work, and i cant seem to figur out the wildcard, it just adds more errors xD – user2340383 Jul 25 '14 at 11:15
  • Sorry for all these problems, but if i change src to src = "E:\*.*", it says thats its an invalid filename (IOError: [Errno 22] invalid mode ('rb') or filename: 'E:\\*.*') – user2340383 Jul 25 '14 at 11:20
  • Are you sure you typed backslash after E: ? – LetMeSOThat4U Jul 25 '14 at 11:26
  • Yes, it just removes it in the comment, it has backslash after the E, and in the error it has backslashes instead of the shown in the comment – user2340383 Jul 25 '14 at 11:34
  • \ is an escape key, so try `E:\\*.*` instead. – TheDarkTurtle Jul 25 '14 at 11:53
  • Error: Traceback (most recent call last): File "C:\Users\DC\Desktop\Prototype\Prototype", line 10, in shutil.move(src, dst) File "C:\Python27\lib\shutil.py", line 302, in move copy2(src, real_dst) File "C:\Python27\lib\shutil.py", line 130, in copy2 copyfile(src, dst) File "C:\Python27\lib\shutil.py", line 82, in copyfile with open(src, 'rb') as fsrc: IOError: [Errno 22] invalid mode ('rb') or filename: 'E:\\*.*' – user2340383 Jul 25 '14 at 12:46
0

Please check permissions to E:\.

Error shows, user which runs this code snippet, has no permission to E drive.

Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • 1
    I am moving from, and there are files, and manually i can just move everything in there.. Thats why i dont really get it :/ – user2340383 Jul 25 '14 at 11:08