basic .json storage and retrieval [duplicate] - python

I need to put a lot of filepaths in the form of strings in Python as part of my program. For example one of my directories is D:\ful_automate\dl. But Python recognizes some of the characters together as other characters and throws an error. In the example the error is IOError: [Errno 22] invalid mode ('wb') or filename: 'D:\x0cul_automate\\dl. It happens a lot for me and every time I need to change the directory name to one that may not be problematic.

The \ character is used to form character escapes; \f has special meaning.
Use / or use raw string r'' instead. Alternatively, you could ensure that Python reads the backslash as a backslash by escaping it with an additional \.
r'D:\ful_automate\dl'
'D:\\ful_automate\\dl'
'D:/ful_automate/dl'
Demo to show the difference:
>>> 'D:\ful_automate\dl'
'D:\x0cul_automate\\dl'
>>> r'D:\ful_automate\dl'
'D:\\ful_automate\\dl'

Use raw string instead of string ie
use r'filepath'
It fixes the problem off blacklash "\"

Related

latexcodec stripping slashes but not translating characters (Python)

I'm trying to process some Bibtex entries converted to an XML tree via Pybtex. I'd like to go ahead and process all the special characters from their LaTeX specials to unicode characters, via latexcodec. Via question Does pybtex support accent/special characters in .bib file? and the documentation I have checked the syntax, however, I am not getting the correct output.
>>> import latexcodec
>>> name = 'Br\"{u}derle'
>>> name.decode('latex')
u'Br"{u}derle'
I have tested this across different strings and special characters and always it just strips off the first slash without translating the character. Should I be using latexencoder differently to get the correct output?
Your backslash is not included in the string at all because it is treated as a string escape, so the codec never sees it:
>>> print 'Br\"{u}derle'
Br"{u}derle
Use a raw string:
name = r'Br\"{u}derle'
Alternatively, try reading actual data from a file, in which case the raw/non-raw distinction will not matter. (The distinction only applies to literal strings in Python source code.)

Python reads \\ when \ is the input

I am trying to create a new text file in an empty folder. The path to the folder is:
C:\Users\Tor\Desktop\Python files\retning
When I type this in the command line in windows explorer, I get straight to the empty folder.
When I type my code in Python I get an errormessage and it looks like Python has replaced a couple of the '\' with '\\'
This is my code
sector='A5'
g=open('C:\Users\Tor\Desktop\Python files\retning\retning'+sector+'.txt', 'a')
and this is the errormessage
Traceback (most recent call last):
File "C:\Users\Tor\Desktop\Python files\filer som behandler output\Vindretning.py", line 2, in <module>
g=open('C:\Users\Tor\Desktop\Python files\retning\retning'+sector+'.txt', 'a')
IOError: [Errno 22] invalid mode ('a') or filename: 'C:\\Users\\Tor\\Desktop\\Python files\retning\retningA5.txt'
Can anyone please tell me what I am doing wrong, or what is happening here?
\ needs to be escaped in the strings. That is why \\ or raw strings are used (r'test String')
Using raw strings solves the problem here. Something like,
open(r'C:\Programming Test Folder\test_file.py')
So, your code gets changed to
g=open(r'C:\Users\Tor\Desktop\Python files\retning\retning{}.txt'.format(sector), 'a')
Or use / in Windows, like follows
g=open('C:/Users/Tor/Desktop/Python files/retning/retning'+sector+'.txt', 'a')
This is normal behaviour; Python is giving you a string representation that can be pasted right back into a Python script or interpreter prompt. Since \ is a character used in Python string literals to start an escape sequence (such as \n or \xa0) the backslashes are doubled.
In fact, it is the characters without escaped backslashes that are the key here; \r is the escape code for a carriage return. You need to use one of the following options to specify Windows paths instead:
Escape all backslashes by doubling them in your string literals:
g = open('C:\\Users\\Tor\\Desktop\\Python files\\retning\\retning'+sector+'.txt', 'a')
Now the \r won't be interpreted as an escape code.
Use a raw string literal:
g = open(r'C:\Users\Tor\Desktop\Python files\retning\retning'+sector+'.txt', 'a')
In raw string literals most escape codes are ignored.
Use forward slashes:
g = open('C:/Users/Tor/Desktop/Python files/retning/retning'+sector+'.txt', 'a')
Forward slashes work fine as path separators on Windows, and there's no chance of them being interpreted as escape characters.
In a normal python string, a backslash can have a special meaning (for instance, \n indicates a new line). In the path you've provided in your code, you either need to use \\ for each directory separator (\\ means include a ), or mark the string as a raw string, meaning the special treatment for backslashes doesn't apply. You do that with an r before the quote mark, like r'Folder\Sub-Folder\Another'
The error message is basically python giving you the python code you can use to get your original string.

error "invalid filename" if i use letters a to f in file name

i'm using pyton 2.7. I have written a script , when it is executed, it will call and run some other file with the name abc.py. but i'm getting error
IOError: [Errno 22] invalid mode ('r') or filename: 'F:\x07bc.c'
it is working fine if i change the file name.
it shows error only if i use letters from a to f as the first letter of file name
Please help.
Thank you
Consider the Python string '\a'. As described in the documentation, the back slash character is interpreted as an escape character. So '\a' is in fact the ASCII Bell character, character number 7.
Your filename is 'F:\abc.c' and the \a in there is interpreted as ASCII Bell. You can see this clearly in the interpretor:
>>> 'F:\abc.c'
'F:\x07bc.c'
>>> print 'F:\abc.c'
F:bc.c
When you print that string note that the \a does not appear. That's because it has been turned into a Bell control character which is invisible.
To include a backslash you can use the correct escape sequence \\. Put it all together and your filename should be: 'F:\\abc.c'. As an alternative, you can prefix the string with r to make it a raw string. This is also detailed in the documentation.
>>> 'F:\\abc.c'
'F:\\abc.c'
>>> print 'F:\\abc.c'
F:\abc.c
>>> r'F:\abc.c'
'F:\\abc.c'
>>> print r'F:\abc.c'
F:\abc.c
Try this:
open(r'F:\abc.c')
i.e. add r before quotes.
UPDATE Sorry, I misinterpreted the code (although my solution is correct). #DavidHeffernan is right, the \a is read as the ASCII bell.
Escape the \ with another backslash, like this:
print 'F:\\x07bc.c'

File paths in Python in the form of string throw errors

I need to put a lot of filepaths in the form of strings in Python as part of my program. For example one of my directories is D:\ful_automate\dl. But Python recognizes some of the characters together as other characters and throws an error. In the example the error is IOError: [Errno 22] invalid mode ('wb') or filename: 'D:\x0cul_automate\\dl. It happens a lot for me and every time I need to change the directory name to one that may not be problematic.
The \ character is used to form character escapes; \f has special meaning.
Use / or use raw string r'' instead. Alternatively, you could ensure that Python reads the backslash as a backslash by escaping it with an additional \.
r'D:\ful_automate\dl'
'D:\\ful_automate\\dl'
'D:/ful_automate/dl'
Demo to show the difference:
>>> 'D:\ful_automate\dl'
'D:\x0cul_automate\\dl'
>>> r'D:\ful_automate\dl'
'D:\\ful_automate\\dl'
Use raw string instead of string ie
use r'filepath'
It fixes the problem off blacklash "\"

Backslashes in Windows filepath? [duplicate]

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
When setting a string to a filepath in Python for WIndows, does it need to be formatted as:
C:\\Users\\
Or do escapes not apply on Windows? My script is currently giving me something like "Non-ASCII character" at the line import os, so I can't really test this.
Try adding an "r", do as below:
path = r"C:\mypaht\morepaht\myfie.file"
Short answer: Use forward slash instead as suggested by gnibbler.
On using raw strings:
Using a raw string usually works fine, still you have to note that r"\"" escapes the quoute char. That is, raw string is not absolutely raw and thats the reason why you cant use backslash (or any odd number of backslashes) in the end of a string like '\' (the backslash would escape the following quote character).
In [9]: a=r'\\'
In [10]: b=r'\\\'
File "<ipython-input-10-9f86439e68a3>", line 1
b=r'\\\'
^
SyntaxError: EOL while scanning string literal
In [11]: a
Out[11]: '\\\\'
You should not construct file paths that way. Its not portable and error prone.
Use the join() function from os.path
import os.path
path = os.path.join('C:', 'Users', 'name')

Categories

Resources