0

I am trying to use a shutil script I found but it receives SyntaxError: unterminated string literal (detected at line 4). Any assistance would be appreciated to fixing this or new script

import shutil
import os

source = r"C:\Users\[username]\Downloads\"
dest1 = r" C:\Users\[username]\Desktop\Reports\14"
dest2 = r" C:\Users\[username]\Desktop\Reports\52"
dest3 = r" C:\Users\[username]\Desktop\Reports\59"

files = os.listdir(source)

for f in files:
   
 if (f.startswith("Log 14")):
        shutil.move(f, dest1)
    elif (f.startswith("Log 54")):
        shutil.move(f, dest2)
Ric
  • 11
  • 1
  • 2
  • **it is not working** is not something we can not help you with. Be more specific. – Patrick Artner Dec 09 '21 at 20:46
  • I had received SyntaxError: unterminated string literal (detected at line 4) error. – Ric Dec 09 '21 at 21:02
  • You DO see the different colored rendering? that is partly because \" is interpreted ans escaping the " .. but you also have SLANTED " in your code. Fix it and try again. – Patrick Artner Dec 09 '21 at 21:25
  • Note that this is a new error message in Python 3.10. The old error message was `SyntaxError: EOL while scanning string literal` (at least as far back as 2.7). – wjandrea Dec 09 '21 at 21:53
  • Does this answer your question? [In python SyntaxError: EOL while scanning string literal](https://stackoverflow.com/questions/39323050/in-python-syntaxerror-eol-while-scanning-string-literal) – wjandrea Dec 09 '21 at 22:01
  • BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Dec 09 '21 at 22:02
  • 1
    I just noticed @John overhauled the question, which might be jarring for a newbie. The reason is that SO is meant for questions about specific technical problems, and questions that amount to ["Can someone help me?"](https://meta.stackoverflow.com/q/284236/4518341) are not helpful. So John changed the focus to the immediate problem. See [ask]. – wjandrea Dec 09 '21 at 22:14
  • 1
    Thanks @wjandrea. That's exactly right. Ric, if you have additional problems I recommend you post a new question so that each post can be a single problem and its solution. Of course, try to debug them on your own first. It's best not to ask SO until you've given it the old college try yourself. – John Kugelman Dec 09 '21 at 23:56

3 Answers3

1

Watch out for smart quotes . They need to be double quotes ".

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
1

You had smart quotes instead of normal ones. Indenting is also not correct.

Here is the fixed code:

import shutil
import os

source = "C:\\Users\\[username]\\Downloads\\"
dest1 = "C:\\Users\\[username]\\Desktop\\Reports\\14"
dest2 = "C:\\Users\\[username]\\Desktop\\Reports\\52"
dest3 = "C:\\Users\\[username]\\Desktop\\Reports\\59"

files = os.listdir(source)

for f in files:
    if f.startswith("Log 14"):
        shutil.move(source + f, dest1)
    elif f.startswith("Log 54"):
        shutil.move(source + f, dest2)
wjandrea
  • 23,210
  • 7
  • 49
  • 68
norbot
  • 127
  • 6
  • My bad that was a typo on my side, I have corrected it however, the script still unsuccessful. After running the script the files did not move and also did not received an error. – Ric Dec 09 '21 at 22:09
  • They worked correctly for me. Do your filenames start with the given strings? (Log 14, etc.) – norbot Dec 09 '21 at 22:21
0

Smart Quotation Marks strike again.

Using BabelStone, one can determine the unicode identification of each character in your code.

The start/ending quotes you're used to are \U0022. However, the end of the URL on dest2 ends with a different character, which is \U201D. This is a different character. Easiest way to fix this is to retype out the quotation marks in your IDE.

Input: "”

U+0022 : QUOTATION MARK {double quote}
U+201D : RIGHT DOUBLE QUOTATION MARK {double comma quotation mark}
blackbrandt
  • 1,768
  • 14
  • 29