9

I am trying to use a shutil script I found, but running it triggers:

SyntaxError: unterminated string literal (detected at line 4)

This is maybe related to the script containing raw windows paths. How do I fix the 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)
9
  • it is not working is not something we can not help you with. Be more specific. Commented Dec 9, 2021 at 20:46
  • I had received SyntaxError: unterminated string literal (detected at line 4) error. Commented Dec 9, 2021 at 21:02
  • 2
    Does this answer your question? In python SyntaxError: EOL while scanning string literal Commented Dec 9, 2021 at 22:01
  • 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?" are not helpful. So John changed the focus to the immediate problem. See How to Ask. Commented Dec 9, 2021 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. Commented Dec 9, 2021 at 23:56

4 Answers 4

6

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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.
They worked correctly for me. Do your filenames start with the given strings? (Log 14, etc.)
3

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

1 Comment

I think the smart quote was a typo in the question ... not the real issue. It fails even with dumb quotes.
3
import os

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


source = "C:"+a+"Users"+a+"[username]"+a+"Downloads"+a

3 Comments

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 How to Answer.
(Though note that Python is perfectly happy to use forward slashes as directory separators on Windows.)
This answer was flagged as Low Quality, and could benefit from an explanation. Here are some guidelines for How do I write a good 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
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}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.