Skip to content
Fix Code Error

“Unicode Error “unicodeescape” codec can’t decode bytes… Cannot open text files in Python 3

March 13, 2021 by Code Error
Posted By: Anonymous

I am using Python 3.1 on a Windows 7 machine. Russian is the default system language, and utf-8 is the default encoding.

Looking at the answer to a previous question, I have attempting using the "codecs" module to give me a little luck. Here’s a few examples:

>>> g = codecs.open("C:UsersEricDesktopbeeline.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated U✘✘✘✘✘✘XX escape (<pyshell#39>, line 1)
>>> g = codecs.open("C:UsersEricDesktopSite.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated U✘✘✘✘✘✘XX escape (<pyshell#40>, line 1)
>>> g = codecs.open("C:Python31Notes.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 11-12: malformed N character escape (<pyshell#41>, line 1)
>>> g = codecs.open("C:UsersEricDesktopSite.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated U✘✘✘✘✘✘XX escape (<pyshell#44>, line 1)

My last idea was, I thought it might have been the fact that Windows "translates" a few folders, such as the "users" folder, into Russian (though typing "users" is still the correct path), so I tried it in the Python31 folder. Still, no luck. Any ideas?

Solution

The problem is with the string

"C:UsersEricDesktopbeeline.txt"

Here, U in "C:Users… starts an eight-character Unicode escape, such as U00014321. In your code, the escape is followed by the character ‘s’, which is invalid.

You either need to duplicate all backslashes:

"C:\Users\Eric\Desktop\beeline.txt"

Or prefix the string with r (to produce a raw string):

r"C:UsersEricDesktopbeeline.txt"
Answered By: Anonymous

Related Articles

  • How to properly do JSON API GET requests and assign…
  • Azure Availability Zone ARM Config
  • How to parse JSON with XE2 dbxJSON
  • The 'compilation' argument must be an instance of…
  • Octave using 'for' statement to show two animations…
  • Search match multiple values in single field in…
  • NullpointerException error while working with…
  • Avoid creating new session on each axios request laravel
  • Event Snippet for Google only shows one event while…
  • Why does this Azure Resource Manager Template fail…
  • loop and eliminate unwanted lines with beautiful soup
  • mongodb group values by multiple fields
  • What is the worst programming language you ever worked with?
  • How do I limit the number of digits from 6 to 4 in…
  • Most effective way to parse JSON Objects
  • Azure CLI - az deployment group create -…
  • Does moment.js allow me to derive a timezone…
  • What encoding/code page is cmd.exe using?
  • ./components/Avatar.tsx Error: Cannot find module…
  • How to traverse the complex nested Json in C# and…
  • C# Get YouTube videoId from Json
  • Python scrape JS data
  • Angular 12 - Generating browser application bundles…
  • Python JSON TypeError : list indices must be…
  • error NG6002: Appears in the NgModule.imports of…
  • Can't upload files with Apollo-client GraphQL in…
  • Error Stack Overflow when trying to hide buttons in…
  • Siemens LOGO! PLC data in the wrong order
  • How to build correlation matrix plot using specified…
  • Need constraints for y position or height when…
  • python pandas - add unique Ids in column from master…
  • How do I remove single children in a tree?
  • What are the undocumented features and limitations…
  • One to many relationship in two JSON arrays, lookup…
  • Posting array of objects to REST API with ReactJS
  • TypeError: players.map is not a function
  • How to query Json field so the keys are the column…
  • Function runs into an error after successfully…
  • Python is not calling fucntions properly
  • Scraping text after a span in with Regex (and Requests)
  • Blazor Can't Update UI
  • Fix top buttons on scroll of list below
  • state data can not be set using useState in reactjs
  • C# parse FHIR bundle - read resources
  • Fragment Not Showing in the Activity
  • Python - Read JSON - TypeError: string indices must…
  • How to blur the background after click on the button…
  • how to transform a JSON coming from an API into…
  • Remove Text from the edit text when edit text is focused
  • Need to split output into an javascript array
  • Vega-lite data transformation to un-nest objects
  • Simplest way to create Unix-like continuous pipeline…
  • How can i use `parse = T` and functions like round…
  • I want to create a SQLite database like in the…
  • How to determine the current iPhone/device model?
  • Get operating system info
  • R - Apply function to list of lm summaries
  • How to format a phone number in a textfield
  • How to add Typescript to a Nativescript-Vue project?
  • Best practice multi language website
  • MaterialCardView is in front of NavigationView. How…
  • Conditional render label Color base on another field value
  • Python Inserting headers for CSV file when…
  • Split JSON python string to pass to function
  • How can I center all my contents in html?
  • python encoding utf-8
  • How do we split words from a html file using string…
  • How do I get currency exchange rates via an API such…
  • Combining pyOSC with pyQT5 / Threading?
  • How to reverse $unwind or re-assemble after $lookup?
  • Js Calculation based on radio button, and some problems
  • How to use Servlets and Ajax?
  • Errorbars and bar plots having different positions in ggplot
  • Azure Sql : Unable to Replace HTML String
  • JSON Schema - How to apply conditionals based on…
  • React function keeps refreshing page causing huge…
  • Javascript filtering a nested array to exclude…
  • Error: the entity type requires a primary key
  • Updating products with fetch and input value fields
  • Install slick carousel in VueJS project
  • ComboBox.SelectedItem giving Null value
  • What to change in circular barplot in R?
  • ng serve is checking nodemodules in previous directory
  • Svelte application not working on android version…
  • Why is my Shopify App built with Next.js (React) so…
  • How can I find the product GUID of an installed MSI setup?
  • AWS CodeCommit With Multi-factor Authentication.…
  • Unable to move from one fragment to another
  • ggplot by group with filter()
  • Changing PowerShell's default output encoding to UTF-8
  • Merge list of results into a single variable with Python
  • Why so much unused space, engine is skipping to a…
  • How to get device make and model on iOS?
  • My node.js json parse function throws error
  • Issues trying to install sylius/product-bundle to my…
  • How do I modify fields inside the new PostgreSQL…
  • Reference — What does this symbol mean in PHP?
  • How to read through or navigate through a json object?
  • Generate sequence of dates for given frequency as…
  • onClick for buttons on custom CardView

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to update gradle in android studio?

Next Post:

What are the differences between type() and isinstance()?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error