user2130490
user2130490

Reputation: 1

IOError: [Errno 22] invalid mode ('rb') or filename

I am a beginner at python and trying to use a very simple shutil module (shutil.copy) to copy databases from multiple folders into a backup folder. I am getting the error below. Any help is appreciated.

# importing os module  
import os

#import time module
import time
import datetime

# importing shutil module  
import shutil 


now = datetime.datetime.now()
timestamp = str(now.strftime("%Y%m%d_%H%M%S"))

source5 = "F:/SHARED/SOP/PRE GO LIVE/TEST CASES & SCENARIOS/MASTER/PRE_GO_LIVE_MASTER.accdb"

dest5 = "F:/SHARED/SOP/SB/Python/Destination/PRE_GO_LIVE_MASTER.accdb_"+timestamp+".accdb" 

print("Before copying ")

DB5 = shutil.copy(source5,dest5)

print("After DATABASE has been copied")

Error:
Traceback (most recent call last):
  File "C:\Users\sbasava1\Desktop\Python\Final_Attempt.py", line 101, in <module>
    DB5 = shutil.copy(source5,dest5)
  File "C:\Python27\lib\shutil.py", line 119, in copy
    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: 

Upvotes: 0

Views: 2170

Answers (1)

Brendan Jennings
Brendan Jennings

Reputation: 56

Check your file path, use double backslashes \\ or a single forward slash / or make your string a raw string r"...".

# Original path - wouldn't work
path = "c:\location\directory"

# Raw string - would work
path = r"c:\location\directory"

# Double slashes - would work
path = "c:\\location\\directory"

# Forward slashes - would work
path = "c:/location/directory"

Consider learning about string literals

If this didn't help leave me a comment, it would also help to see the/part of the code you are working with!

Edit: Running your script didn't give me an issue:

Screenshot

Check if the directory you are trying to create a file in actually exists

Upvotes: 1

Related Questions