0

This is one of my lines in a script I'm trying to make in Python:

src = r"C:\Users\Aydan\Desktop\SortedImages\" + text_file_name + "\\" + file_name

As you can see, I'm trying to concatenate a file destination together with folder names stored in a variable and a file name stored in another variable.

When debugging, I'm getting an error with the syntax:

SyntaxError: unexpected character after line continuation character

I'm assuming it's the backslash, but I've escaped it so I'm not too sure what to do. Other than the backslash, I have no idea what this error is pointing at.

Whole code:

import shutil
import glob
dst = r"C:/Users/Aydan/Desktop/1855"

for filename in glob.glob(r"C:\Users\Aydan\Desktop\RTHPython\Years\*.txt"):
    text_file_name = filename.strip()
    with open (text_file_name) as my_file:
        for filename in my_file:
            file_name  = filename.strip()
            src = r"C:\Users\Aydan\Desktop\SortedImages\" + text_file_name + "\\" + file_name
            shutil.move(src, dst)
Aydan Howell
  • 75
  • 1
  • 12
  • 1
    You can't put a single backslash as the final char in a raw string. But you shouldn't be using string concatenation to build paths. The old way to do it properly is [`os.path.join`](https://docs.python.org/3/library/os.path.html#os.path.join), the modern way is to use the [`pathlib`](https://docs.python.org/3/library/pathlib.html) module. – PM 2Ring Jul 21 '17 at 10:35

1 Answers1

4

A string can't end with a backslash which essentially escapes the closing quote, even if using raw strings:

src = r"C:\Users\Aydan\Desktop\SortedImages\"
  File "<stdin>", line 1
    src = r"C:\Users\Aydan\Desktop\SortedImages\"
                                                ^
SyntaxError: EOL while scanning string literal

Simply use os.path.join:

import os

os.path.join(r"C:\Users\Aydan\Desktop\SortedImages", text_file_name, file_name)
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Why doesn't marking the string as `raw` prevent escaping the quotation mark at the end of the string? That seems odd in my opinion. – Tom Wyllie Jul 21 '17 at 10:35
  • Check all my code, just edited. – Aydan Howell Jul 21 '17 at 10:36
  • 1
    @TomWyllie See https://stackoverflow.com/q/647769/4014959 – PM 2Ring Jul 21 '17 at 10:37
  • @PM2Ring, that's really obvious actually now I've read that, can't believe I missed that! – Tom Wyllie Jul 21 '17 at 10:40
  • Does this search for the files named? Or does it need a file to take the files out of? – Aydan Howell Jul 21 '17 at 10:41
  • @AydanHowell what do you mean? this does the exact same thing your code tried to do. Just replace `src = r"C:\Users\Aydan\Desktop\SortedImages\" + text_file_name + "\\" + file_name` in your code with the line in my answer (and don't forget `import os`) – DeepSpace Jul 21 '17 at 10:42
  • @AydanHowell `os.path.join` just builds the path, you can read its docs in the link in my first comment. But I seriously suggest you take a look at the `pathlib` module, it's simply awesome. – PM 2Ring Jul 21 '17 at 10:46
  • @DeepSpace Read the bottom of my original post, the files are moved from a named folder, and moved to another folder. Does this `os` code do that? – Aydan Howell Jul 21 '17 at 10:46
  • @PM2Ring I'm reading it as we speak. Thanks! – Aydan Howell Jul 21 '17 at 10:47
  • @PM2Ring It is only available in Python 3.6 – DeepSpace Jul 21 '17 at 10:47
  • @AydanHowell No. it doesn't move anything. It simply builds the path string your code tried to build by concatenating.... – DeepSpace Jul 21 '17 at 10:48
  • @DeepSpace True, but 3.6 has been available for a while now... ;) – PM 2Ring Jul 21 '17 at 10:49
  • @DeepSpace then how do I move those files? Read my code. – Aydan Howell Jul 21 '17 at 10:50
  • @AydanHowell I really don't understand the issue here. You already have the code that moves the file (`shutil.move(..)`) – DeepSpace Jul 21 '17 at 10:51
  • @DeepSpace Oh I understand, I assumed you wanted me to replace shutil with os in a sense. How can I use that path as a source? Like this `src = os.path.join(r"C:\Users\Aydan\Desktop\SortedImages", text_file_name, file_name)` – Aydan Howell Jul 21 '17 at 10:53