Member Avatar for Mamatha_1
Hi All,
I have a task in Python where I need to move .csv files into a new folder. I tried using the below code but it was throwing me an error.

import shutil
import os
source = os.listdir(r'C:\Users\mchalla\PycharmProjects\837_unzip\venv\SampleUnzipFiles\SampleZipFiles/')
destination = 'C:\Users\mchalla\PycharmProjects\837_unzip\venv\SampleUnzipFiles\DelimitedFiles'
for files in source:
        if files.endswith("_new.csv"):
                shutil.move(files,destination)

 Error Message:
C:\Users\mchalla\PycharmProjects\837_unzip\venv\Scripts\python.exe C:/Users/mchalla/PycharmProjects/837_unzip/MoveFilesToNewFolder.py
 File "C:/Users/mchalla/PycharmProjects/837_unzip/MoveFilesToNewFolder.py", line 4
 destination = 'C:\Users\mchalla\PycharmProjects\837_unzip\venv\SampleUnzipFiles\DelimitedFiles'
             ^
 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Process finished with exit code 1

Could someone please help me to solve this issue?

Thank you!

Recommended Answers

All 4 Replies

Poor post formatting aside, could the back and forward slash casue issues in line 6?
What is that letter r doing in line 6's parameter?
Do you need a trailing slash in line 7?

Member Avatar for Mamatha_1

Yes I need all the mentioned strings or characters that are in the above code:
r in line 6 denotes a raw string
trailing slash in line 7 denotes that I have multiple files in the folder with multiple extensions.

You need to escape your slashes in string literals.

'C:\Users\mchalla\PycharmProjects\837_unzip\venv\SampleUnzipFiles\DelimitedFiles'

should be

'C:\\Users\\mchalla\\PycharmProjects\\837_unzip\\venv\\SampleUnzipFiles\\DelimitedFiles'

or, you know.. be consistent and just flag it also as a raw string :-P
r'C:\Users\mchalla\PycharmProjects\837_unzip\venv\SampleUnzipFiles\DelimitedFiles'

The error you are getting is from \U telling Python that the next series of values represents a unicode value (in your case "sers" which, FAIK, converts to the ascii values and then tries to high-bit/low-bit them and errors out).

//More info!
from: https://docs.python.org/3/reference/lexical_analysis.html

Escape sequences only recognized in string literals are:

Escape Sequence Meaning Notes
\N{name} Character named name in the Unicode database (4)
\uxxxx Character with 16-bit hex value xxxx (5)
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (6)

so I wasn't far off... it didnt high-bit/low-bit it just straight up tried to convert them to a 32 bit value and puked on itself because it was out of bounds.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.