Unable to open txt file in python - python

Whenever I try to access .txt file using python, I get an error:
Traceback (most recent call last):
File "C:/Users/monty/AppData/Local/Programs/Python/Python38-32/tess.py", line 1, in
f1=open("d:\test.txt")
OSError: [Errno 22] Invalid argument: 'd:\test.txt'
Please help!

Specify the path using double backward slash instead of single slash(i.e backward slash is escape charactor)
fp = open('D:\\test.txt')
use single forward slash
fp = open('D:/test.txt')

In normal Python string literals, a backslash followed by some character has a special meaning. (Search for "string literals" and "escape sequence" or see here.) In this case, \t is the tab character. Try this:
>>> print("d:\test.txt")
d: est.txt
So, when you try to open "d:\test.txt", you're not opening the file test.txt in the root directory of the d drive but the file dtabtest.txt in the current working directory.
There are multiple solutions to this. E.g. use a raw string literal: r"d:\test.txt".

In python open() function basically needs 3 parametres: filename,mode and encoding. You have specified the filename but you still need to specify the mode. There are many modes but you will basically need 3 of them:
"w" ->(write mode) creates file and if file exists creates empty file
"a" ->(append mode) creates file and if file exists appends new data to previous data
"r" ->(read mode) reads precreated file.
and encoding parametre is for seeing the chracters correct:
ascii -> for english alphabet
utf-8 -> for most of the langugages in the world (use this)
So all in all you should use open function like this choose which fits your purpose:
fp=open("filename.txt","w",encoding="utf-8")
fp=open("filename.txt","a",encoding="utf-8")
fp=open("filename.txt","r",encoding="utf-8")

Related

Python adds backslashes and quotes to user input file path, won't open

Using Python 3.9, I want to "input" a file path (via the input function). Then I want to open that file path. I am hindered by the following error:
OSError: [Errno 22] Invalid argument: "'C:\\\\Users\\\\Hart\\\\Documents\\\\File.txt'"
I'm wondering why.
Here's what I know: I have a file path. According to os.path.exists, the file path exists.
>>> os.path.exists("C:\\Users\\Hart\\Documents\\File.txt")
True
I can open the file.
>>> open("C:\\Users\\Hart\\Documents\\File.txt")
<_io.TextIOWrapper name='C:\\Users\\Hart\\Documents\\File.txt' mode='r' encoding='cp1252'>
I can call the file anything, and it still exists, and I can still open it.
>>> anything = "C:\\Users\\Hart\\Documents\\File.txt"
>>> os.path.exists(anything)
True
>>> open(anything)
<_io.TextIOWrapper name='C:\\Users\\Hart\\Documents\\File.txt' mode='r' encoding='cp1252'>
But I want to open the file indirectly, via the input function.
>>> file = input('Enter a file path\n')
Enter a file path
'C:\\Users\\Hart\\Documents\\File.txt'
This creates a variable called file, which resembles my file path.
>>> print(file)
'C:\\Users\\Hart\\Documents\\File.txt'
But when I try to open it...
OSError: [Errno 22] Invalid argument: "'C:\\\\Users\\\\Hart\\\\Documents\\\\File.txt'"
Nor does the file represented by this variable exist.
>>> os.path.exists(file)
False
What's going on?
As described in String and Bytes Literals python string literals recognize escape sequences starting with the backslash (\) to encode things like newlines (\n), hex values (\x03) and etc. Notice that these rules apply to string literals, that is, strings in python source code that is compiled by python before use.
Strings read from other sources aren't literals and don't follow those rules. So, a string in python source code
file = "C:\\Users\\Hart\\Documents\\File.txt"
is really
C:\Users\Hart\Documents\File.txt
in memory. It can be hard to spot this because when you display the string, python may show you an escaped literal representation of the string. You can see the difference with a quick shell example
>>> test = "C:\\Users\\Hart\\Documents\\File.txt"
>>> test
'C:\\Users\\Hart\\Documents\\File.txt'
>>> print(test)
C:\Users\Hart\Documents\File.txt
When you input() text, that is the real text, no a string literal. Windows doesn't want the quotes or the extra backslashes. So,
>>> file = input('Enter a file path\n')
Enter a file path
C:\Users\Hart\Documents\File.txt
>>> print(file)
C:\Users\Hart\Documents\File.txt

How do I write to a file with spaces and special characters in file name?

I'm trying to write to a file called
data/STO: INVE-B-TIME_SERIES_DAILY_ADJUSTED-1.csv
using the following python code on windows
overwrite = "w" # or write new
f = open(OutputPath(copyNr), overwrite)
f.write(response.content.decode('utf-8'))
f.close()
print(f"Wrote to file: {OutputPath(copyNr)}")
The output in the console is correct, but the file written to results in only data/STO so the path seems to be clipped. I've tried to escape the characters using the method from this SO answer but that gave me an invalid_argument exception for the following filename:
data/STO\\:\\ INVE-B-TIME_SERIES_DAILY_ADJUSTED-1.csv
I get the same error when I remove the space and it still seems to clip at :. How do I include such characters in a filename?
You can't. Colons are only allowed on the volumeseperator - anywhere else there are illegal.
Allowed:
d:/somedir/somefile.txt
Illegal:
./some:dir/somefile.txt
and as seperators you can either use '\\' or '/' - both should be understood.
This is not a python limitation but a operating system limitation.
See f.e. What characters are forbidden in Windows and Linux directory names?
Windows API will not allow colon in filename, it is reserved for drive letter, use a different sign.

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.

What's the proper way to open a file in python 2.7?

I'm trying to open a txt file in IDLE but it gives me an error.
I can't figure out what happens to the f in my file name or why the single '\' becomes double in the error message.
>>>f=open('D:\programs\python 2.7.10\programs\foo.txt','r')
Traceback (most recent call last):
File "<pyshell#94>", line 1, in <module>
f=open('D:\programs\python 2.7.10\programs\foo.txt','r')
IOError: [Errno 22] invalid mode ('r') or filename: 'D:\\programs\\python 2.7.10\\programs\x0coo.txt'
Backslashes are used for escape sequences - in your case the culprit is \f which is the form-feed character. You can also use forward slashes on modern Windows systems as well as an alternative.
Use a raw string:
f=open(r'D:\programs\python 2.7.10\programs\foo.txt','r')
Ideally though, you should use the with statement so that it automatically closes the file in case of exceptions or when the with block exits, eg:
with open(r'D:\programs\python 2.7.10\programs\foo.txt','r') as f:
# do stuff with `f`
You have a funny character "\x0c" in your path. Its "f" in hex. Python doesn't understand. That's why ASCII gives an error. Rename your file into something nicer and you will be fine.

'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