2

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
  • 31
  • 1
  • 1
  • 3
  • **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
  • 2
    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
  • 2
    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

4 Answers4

2

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
2

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
  • 28,235
  • 9
  • 60
  • 81
norbot
  • 187
  • 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
1
import os

if os.name == 'nt':    #check if windows 
  a='\\'
else:
  a='/'


source = "C:"+a+"Users"+a+"[username]"+a+"Downloads"+a
RITESH
  • 125
  • 1
  • 5
  • 2
    Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your answer and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike Jul 07 '22 at 13:57
  • (Though note that Python is perfectly happy to use forward slashes as directory separators on Windows.) – ChrisGPT was on strike Jul 07 '22 at 13:58
  • This answer was flagged as [Low Quality](https://stackoverflow.com/help/review-low-quality), and could benefit from an explanation. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32192777) – Trenton McKinney Jul 08 '22 at 17:48
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
  • 2,010
  • 1
  • 15
  • 32