使用open(src,'rb')作为fsrc:IOError:[Errno 22]无效模式('rb')或文件名:

时间:2018-07-01 23:38:25

标签: python-2.7

我要做的是将文件从一个位置更改为另一个位置。

这是代码:

print ("Loading system, please wait 5 seconds.")

try:
    import os, sys
    # Path to be created
    path = "C:\Windows2" # Just a testing random file...
    os.mkdir( path, 0755 );

except:
    print 'Path already created. Passing...'


import shutil
src = data_path =  os.path.expanduser('~')+ '\Desktop\admin.dll'
dst = data_path = "C:\Windows2"
shutil.move(src, dst)

但是,然后出现以下错误:

Traceback (most recent call last):
  File "C:\Users\youse\Desktop\TESTING.py", line 16, in <module>
    shutil.move(src, dst)
  File "C:\Python27\lib\shutil.py", line 316, in move
    copy2(src, real_dst)
  File "C:\Python27\lib\shutil.py", line 144, in copy2
    copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 96, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Users\\youse\\Desktop\x07dmin.dll'
>>> 

1 个答案:

答案 0 :(得分:0)

您需要使用双反斜杠在带引号的字符串中表示文字反斜杠。

src = data_path =  os.path.expanduser('~')+ '\\Desktop\\admin.dll'
dst = data_path = "C:\\Windows2"