0

I am trying to copy and paste a file into my h: drive, which is a personal drive. I am getting the following error:

target = (r'H:\')
SyntaxError: EOL while scanning string literal

Here is my code :

import shutil

original = (r'H:\Bulk upload\filetocopy.xlsx')
target = (r'H:\')
shutil.copyfile(original,target)
Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
rbose
  • 15
  • 1
  • 6

1 Answers1

0

Problem is ending string literal with backslash in single quotes. You should use target = ('H:\\') without the r and with two backslashes. Otherwise you are going to get this type of escape error. If you have the r with two backslashes you will get literally two backslashes.

A second problem (noticed after comment) is that the target is still a directory rather than a valid file path. So you will have to either use shutil.copy instead of shutil.copyfile, or add the file name to the end of the destination path, in which case there is no escape at the end of the string issue.

Andrew Allaire
  • 1,162
  • 1
  • 11
  • 19
  • Hi Andrew - I just tried that, but getting this error: OSError: [Errno 22] Invalid argument: 'H:\\' . I amended the code to this: ```import shutil original = (r'H:\Bulk upload\filetocopy.xlsx') target = ('H:\\') shutil.copyfile(original,target)``` – rbose Dec 15 '20 at 16:08
  • Well yeah this is a second error I did not notice. shutil.copyfile expects the destination to be a file. You should use shutil.copy or add the file name to the destination. – Andrew Allaire Dec 15 '20 at 17:09