Concatenating string and filepath adds extra backslash in pandas - python

This is almost embarrassingly simple, but I couldn't find a way around this at all. My code used to work just fine as well, and nothing fundamental has changed. Honestly, I can't see the mistake.
In the following code whenever I run a pd.read_excel, I get an extra backslash in my concatenation:
sports_data = [pd.read_excel(r'Data\NData' + str(season) + ".xlsx") for season in season_list]
All I want is the filepath beginning, to be tied to the season variable I set earlier in a list.
Thanks, and trust me that I checked high and wide for this!
I've tested the same with double slashes, as well as a raw string. None of them work as each time the backslashes are doubled. I've even separated the filepath, but it still doesn't work
The error I get is the following, each time:
FileNotFoundError: [Errno 2] No such file or directory: 'Data\\NData2002-2003.xlsx'
If I attempt to run a double-backslash to get the line skip to be a simple backslash, it is still doubled:
FileNotFoundError: [Errno 2] No such file or directory: 'Data\\\\NData2002-2003.xlsx'

This behavior is consistent with Python's normal behavior. Try it out in the REPL:
>>> s = r'path\to\my\file'
>>> s
'path\\to\\my\\file'
>>> print(s)
path\to\my\file
A raw string literal still gets stored as a normal string internally, and backslashes still need to be escaped in Python when getting a filepath. Are you sure that the file exists? Double-check the filename and type to see that they match exactly.

Related

The open() functions doesn't behave correctly with filepath containing special characters

I'm writing this simple code:
file = input('File to read: ')
fhand = open(file, 'r')
The file I want to open is called 'test.txt', and it is located in a subfolder; what I put into the requested input therefore is: 'DB\test.txt'.
Well: it doesn't work, returning this error message:
OSError: [Errno 22]Invalid argument: 'DB\test.txt'.
I have another file in the same directory, called 'my_file.txt', and I don't get errors attempting to open it. Lastly I have another file, called 'new_file.txt', and this one also gets me the same error.
What seems obvious to me is that the open() function reads the "\t" and the "\n" as if they were special characters; searching on the web I found nothing that really could help me avoiding special characters within user input strings...
Could anybody help?
Thanks!
you'll have no problems with Python 3 with your exact code (this issue is often encountered when passing windows literal strings where the r prefix is required).
With python 2, first you'll have to wrap your filename with quotes, then all special chars will be interpreted (\t, \n ...). Unless you input r"DB\test.txt" using this raw prefix I was mentionning earlier but it's beginning to become cumbersome :)
So I'd suggest to use raw_input (and don't input text with quotes). Or python version agnostic version to override the unsafe input for python 2 only:
try:
input = raw_input
except NameError:
pass
then your code will work OK and you got rid of possible code injection in your code as a bonus (See python 2 specific topic: Is it ever useful to use Python's input over raw_input?).

Opening file in Python 3.4.2 shell

I am using Python 3.4.2 on Windows 10 and am just getting into opening and read/writing to files from the Python shell
I did this test and got the following error message in spite of the fact that I had created the file beforehand (but not from the shell as that would not work either).
Can someone tell me what I haven't taken into consideration here because all my searches tell me this should work.
>>> import os
>>> helloFile = open('C:\\Users\\jennifer\\Hello.txt')
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
helloFile = open('C:\\Users\\jennifer\\Hello.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\jennifer\\Hello.txt'
I did as John Gordon suggested and removed .txt from the pathname and it still didn't work.
Here is the directory and path for the file:
C:\Users\jennifer\Desktop\Hello
Finally, it has opened and will be very aware of the need to call the complete path in the future! Thank you
Since you have backslashes (\) in the string and since backslashes in string literals mean that a special character is denoted (like \n for newline), you need to make clear that you want verbatim backslashes (as needed by Windows file paths).
For this you have two major options:
You can escape the backslashes with an additional backslash, i. e. effectively double each backslash: 'C:\\foo\\bar'
You can prepend an r to the string to declare it a regexp string in which the special meaning of the backslash is suspended: r'C:\foo\bar'.
There are more options but these are the two major ones used.
Beware, however, that the second option suffers a wart in the Python parser which prevents it to denote a backslash at the end of the string. So if you ever want to have a string literal ending in a backslash (e. g. C:\foo\bar\), then you cannot use this option.
You only need to put a '\' after the Drive, this is to avoid a unicode error. Code below is assuming you have already run Python in Powershell (Window's CMD).
Also if you want to read the file (i.e. print the contents in the CMD) you will need to make it readable by putting r before the filepath.
file =open(r,'C:\\Users\jennifer\Desktop\Hello.txt')
To print the contents:
for i in file:
print(i)
Then hit Enter twice to get your output.

unable to read file from external location in python

I am trying to read a txt file(kept in another location) in python, but getting error.
FileNotFoundError
in ()
----> 1 employeeFile=open("C:‪/Users/xxxxxxxx/Desktop/python/files/employee.txt","r")
2 print(employeeFile.read())
3 employeeFile.close()
FileNotFoundError: [Errno 2] No such file or
directory:'C:\u202a/Users/xxxxxxxx/Desktop/python/files/employee.txt'
Code used:
employeeFile=open("C:‪/Users/xxxxxxxx/Desktop/python/files/employee.txt","r")
print(employeeFile.read())
employeeFile.close()
I tried using frontslash(/) and backslash(). But getting the same error.Please let me know what is missing in code.
I'm guessing you copy and pasted from a Windows property pane, switching backslashes to forward slashes manually. Problem is, the properties dialog shoves a Unicode LEFT-TO-RIGHT EMBEDDING character into the path so the display is consistent, even in locales with right-to-left languages (e.g. Arabic, Hebrew).
You can read more about this on Raymond Chen's blog, The Old New Thing. The solution is to delete that invisible character from your path string. Selecting everything from the initial " to the first forward slash, deleting it, then retyping "C:/, should do the trick.
As your error message suggests, there's a weird character between the colon and the forward slash (C:[some character]/). Other than that the code is fine.
employeeFile = open("C:/Users/xxxxxxxx/Desktop/python/files/employee.txt", "r")
You can copy paste this code and use it.

Python not opening the file i selected

Code:
os.startfile("C:\finished.py")
Return:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\**x0cinished**.py'
Expectation: take C:\finished.py
What could be causing python to change my input like this?
'\f' is a special character (see Table of escape sequences). You should make it a habit to use r (raw strings) when working with hard-coded paths:
os.startfile(r"C:\finished.py")
You need to escape the "\" character. Write "C:\\finished.py" in your startfile statement.

'invalid argument' error opening file (and not reading file)

I am trying to write code that takes 2 numbers in a text file and then divides them, showing the answer as a top heavy fraction. I have gotten the fractions part to work when I am inputting my own values in the program, but i cannot get the program to recognise the text file. I have tried putting them in the same directory and putting the full system path of the file, but nothing so far has worked. right now I am just trying to get the contents of the file to print.
with open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w') as f:
for line in f:
for word in line.split():
print(word)
I will then assign the 2 values to x and y, but I get this error:
Traceback (most recent call last):
File "C:\Python34\divider.py", line 2, in <module>
open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\\ProgramData\\Microsoft\\Windows\\Startmenu\\Programs\\Python 3.4\topheavy.txt'
open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\\ProgramData\\Microsoft\\Windows\\Startmenu\\Programs\\Python 3.4\topheavy.txt'
Two things:
When working with paths that contain backslashes, you either need to use two backslashes, or use the r'' form to prevent interpreting of escape sequences. For example, 'C:\\Program Files\\...' or r'C:\Program Files\...'.
Your error shows this: \\Startmenu\\. It appears that a space is missing between "Start" and "menu", despite the fact that the open line seems to have the right path.
Note: that the \topheavy.txt in your path is probably getting converted to <tab>opheavy.txt too. That's why there aren't two backslashes in front of it in the traceback.
You are using a "\" separator which is probably getting escaped somewhere (like that \t near the end. That's the Windows path separator, but also used as a string escape.
You can double up the "\" as "\". Easiest however is to prepend an r at the beginning to ignore .
r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt"
Skip the recommendation to use / instead, you are not on Unix and there is no reason Python can't accommodate Windows, as long as you remember to take care about "\" also being an escape. Using r' at start also allows you to copy/paste from the string into another program or vice-versa.
also, it wouldn't hurt to test in c:\temp or similar to avoid issues where you may have mistyped your path.
Last, but not least, you need to open in "r" read mode, as previously mentioned.
You should add one more "/" in the last "/" of path for example:
open('C:\Python34\book.csv') to open('C:\Python34\\\book.csv')
Reference
I had this same error appear when trying to read a large file in Python 3.5.4. To solve it, instead of reading the whole file into memory with .read(), I read each line one by one:
with open('big.txt') as f:
for i in f:
print(i)
Just as is written on the Python Documentation, the IOError Exception occurs:
Raised when an I/O operation (such as a print statement, the built-in
open() function or a method of a file object) fails for an I/O-related
reason, e.g., “file not found” or “disk full”.
Open with "r" (read) instead of "w" (write)
And startmenu in these two lines are different?? Try using a forward instead of a back slash. Python will convert the forward slash to the appropriate delimiter for the OS it is running on
open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\ProgramData\Microsoft\Windows\Startmenu\Programs\Python 3.4\topheavy.txt'
Replace every \ with \\ in file path
My issue, rather arbitrary, was I was writing a file using open(filename, "w").write(...), where filename is an invalid pathname or includes unexpected slashes.
For example, converting a datetime.datetime.today() into a datestring with slashes or colons (Windows) and writing to non-existing directories will result in this error.
Change:
open("../backup/2021/08/03 15:02:61.json", "w").write(data)
To:
open("../backup/2021-08-03 15-02-61.json", "w").write(backup)
As an example.

Categories

Resources