0

I'm trying to save a file called 'save_as' as an argument from the command line.

import os.path

script, file1, save_as = argv

...snip...

full_path = os.path.join(r "C:\Users\User\Desktop\Folder_Name\", save_as)

Getting error - SyntaxError: EOL while scanning string literal

Any ideas?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Posteingang
  • 53
  • 1
  • 8

2 Answers2

3

Your issue is caused by the path ending: \.

It is visible in an IDE easily

\ is an escape character which escapes ", hence the "EOL" problem, your path is not "closed", the closing " is interpreted as part of the path, so your first parameter closure will never be found. Remove that \ at the end of path elements,

full_path = os.path.join(r "C:\Users\User\Desktop\Folder_Name", save_as)

should work.

Trapli
  • 1,517
  • 2
  • 13
  • 19
2

Even though, @Trapli is right, I would suggest to start using the pathlib module (new in python 3.4).

>>> from pathlib import WindowsPath
>>> save_as = 'your_file_name.txt'
>>> full_path = WindowsPath('C:/Users/User/Desktop/Folder_Name/') / save_as
>>> str(full_path)
'c:\\Users\\User\\Desktop\\Folder_Name\\your_file_name.txt'

As you can see it has many advantages, for instance:

  • Don't mess up with backslashes when specifying the path
  • Joining paths using / operator

Refer to the docs for more https://docs.python.org/3/library/pathlib.html

slackmart
  • 4,754
  • 3
  • 25
  • 39