I am simply trying to define a path and file name then use pandas.read_csv()
in the variable display of spyder, the path and file name appear correct, but in reality they have double \\. I know this has got to be something really stupid...
siteinfopath=r'C:\Users\cpsei\Documents'
siteinfofile=siteinfopath+'\grav_stats.csv'
grav_stats=pd.read_csv(siteinfofile)
When i run the script I get the following error message:
FileNotFoundError: [Errno 2] File
b'C:\Users\cpsei\Documents\grav_stats.csv' does not exist:
b'C:\Users\cpsei\Documents\grav_stats.csv'
and when I type
siteinfofile
Out[145]: 'C:\\Users\\cpsei\\Documents\\grav_stats.csv'
Why the double \. In the variable viewer the path is correct.
You see double \\ instead of one, because \ is used in python as escape character - it informs that this \ character and next character should be threated in special way. For example:
\t - means TAB
\r - is carriage return - cursor moves to the beginning of the line
\n - is new line - cursor moves to beginning of new line
If however you want just plain simple \, you have to use \\ - first one informs as usual that there is some special character, and next informs that this special character is actually \.
You can read more about it ie on https://docs.python.org/3/tutorial/introduction.html#strings - there is a lot of very good examples :)
So, everything is OK, your strings work as expected. If you want to see how this string looks like, and not how it is constructed, print it:
>>> print(siteinfofile)
C:\Users\cpsei\Documents\grav_stats.csv
Are you sure path is correct and you can read this file? That's the only advice I can think of here...
Related
I have a text file with a path that goes like this:
r"\\user\data\t83\rf\Desktop\QA"
When I try to read this file a print a line it returns the following string, I'm unable to open the file from this location:
'r"\\\\user\\data\\t83\\rf\\Desktop\\QA"\n'
Seems you've got Python code in your text file, so either sanitize your file, so it only includes the actual path (not a Python string representation) or you can try to fiddle with string replace until you're satisfied, or just evaluate the Python string.
Note that using eval() opens Padora's box (it as unsafe as it gets), it's safer to use ast.literal_eval() instead.
import ast
file_content = 'r"\\\\user\\data\\t83\\rf\\Desktop\\QA"\n'
print(eval(file_content)) # do not use this, it's only shown for the sake of completeness
print(ast.literal_eval(file_content))
Output:
\\user\data\t83\rf\Desktop\QA
\\user\data\t83\rf\Desktop\QA
Personally, I'd prefer to sanitize the file, so it only contains \\user\data\t83\rf\Desktop\QA
\ will wait for another character to form one like \n (new line) or \t (tab) therefore a single backslash will merge with the next character. To solve this if the next character is \\ it will represent the single backslash.
my subprocess.call problem is that my shortcut target is with extra INI file which is LIV2.INI and my exe file should run whit it . and my target link in shortcut looks like this
"C:\Program Files (x86)\AMO\EXE\PROGRAM LIVE 2.exe" LIV2.INI
i tried this
subprocess.call('"C:\Users\admin\Desktop\PROGRAM LIVE 2.exe" LIV2.INI')
and i tried this
subprocess.call('C:\Users\admin\Desktop\PROGRAM LIVE 2.exe LIV2.INI')
and i still get error that the ini file missing ? How can i fix this :)
THank you in advance
ERROR : INI FILE Missing or Wrong Name
Please also edit your question to actually include the error since you will get a syntax error, not an error that the ini file is missing.
You have two issues here, first you have a syntax error since "\Us" is not a valid string in python. \u marks the start of a Unicode escape sequence and the character S is not a valid unicode escape key. You can fix this by using double \\ to escape the \ character and tell python you want your string to include a \ and not use it as the start of a escape sequence.
Secondly, subprocess.call excpects a list, not a string (unless you set shell=True; but don't do that, since it means you have to manually escape things which you have already discovered is hard). The first element of the list
is the executable to run and the rest are command line arguments. For example if you wanted to run pythoneand print "hello world" you would type:
subprocess.call(['python', '-c', 'print ("hello world")'])
Notice the missing quotes around the python string? You don't need those since the command line arguments are passed in raw and no shell will attempt to split them if you don't include quotes.
Putting it all together creates something like this:
subprocess.call(['C:\\Users\\admin\\Desktop\\PROGRAM LIVE 2.exe', 'LIV2.INI'])
Notice the double backslashes and how each command line argument is its own list element.
I've been strugling with some code where i need to change simple \ into / in Python. Its a path of file- Python doesn't read path of file in Windows'es way, so i simply want to change Windows path for Python to read file correctly.
I want to parse some text from game to count statistics. Im Doing it this way:
import re
pathNumbers = "D:\Gry\Tibia\packages\TibiaExternal\log\test server.txt"
pathNumbers = re.sub(r"\\", r"/",pathNumbers)
fileNumbers = open (pathNumbers, "r")
print(fileNumbers.readline())
fileNumbers.close()
But the Error i get back is
----> 6 fileNumbers = open (pathNumbers, "r") OSError: [Errno 22] Invalid argument: 'D:/Gry/Tibia/packages/TibiaExternal\test server.txt'
And the problem is, that function re.sub() and .replace(), give the same result- almost full path is replaced, but last char to change always stays untouched.
Do you have any solution for this, because it seems like changing those chars are for python a sensitive point.
Simple answer:
If you want to use paths on different plattforms join them with
os.path.join(path,*paths)
This way you don't have to work with the different separators at all.
Answer to what you intended to do:
The actual problem is, that your pathNumbers variable is not raw (leading r in definition), meaning that the backslashes are used as escape characters. In most cases this does not change anything, because the combinations with the following characters don't have a meaning. \t is the tab character, \n would be the newline character, so these are not simple backslash characters any more.
So simply write
pathNumbers = r"D:\Gry\Tibia\packages\TibiaExternal\log\test server.txt"
This is a code for accessing a file inside a folder using with open() as:{} option.
with open("DATABASE\password.txt") as _2_:
password=_2_.readlines()
with open("DATABASE/names.txt") as _3_:
names=_3_.readlines()
with open("DATABASE\email.txt") as _4_:
email=_4_.readlines()
In this code, if I put "DATABASE\names.txt", as I did for password and email; instead of "DATABASE/names.txt"; it does not work. Please Tell me the reason for the same.
You need to add another backslash. Example: open("path\\to\\file.txt")
Your errors are happening because you need to escape the backslash by adding another one. Such a thing won't happen with /.
You need to escape the \, use raw string r or forward slashes as you have already tried:
"DATABASE\\names.txt" # double \
r"DATABASE\names.txt" # raw string
"DATABASE/names.txt" # use forward slashes
\n is a newline character.
In [7]: print "DATABASE\names.txt" # interpreted as two lines
DATABASE
ames.txt
In [8]: print r"DATABASE\names.txt"
DATABASE\names.txt
A backslash has a special meaning in python, it is used to escape characters.
I'm trying to work with some long file paths (Windows) in Python and have come across some problems. After reading the question here, it looks as though I need to append '\\?\' to the front of my long file paths in order to use them with os.stat(filepath). The problem I'm having is that I can't create a string in Python that ends in a backslash. The question here points out that you can't even end strings in Python with a single '\' character.
Is there anything in any of the Python standard libraries or anywhere else that lets you simply append '\\?\' to the front of a file path you already have? Or is there any other work around for working with long file paths in Windows with Python? It seems like such a simple thing to do, but I can't figure it out for the life of me.
"\\\\?\\" should give you exactly the string you want.
Longer answer: of course you can end a string in Python with a backslash. You just can't do so when it's a "raw" string (one prefixed with an 'r'). Which you usually use for strings that contains (lots of) backslashes (to avoid the infamous "leaning toothpick" syndrome ;-))
Even with a raw string, you can end in a backslash with:
>>> print r'\\?\D:\Blah' + '\\'
\\?\D:\Blah\
or even:
>>> print r'\\?\D:\Blah' '\\'
\\?\D:\Blah\
since Python concatenates to literal strings into one.