I want to open a text file and use split
Here is the code I wrote at first:
with open("test.txt","r") as file:
file.split("\n")
and here is another code I wrote because the first code I wrote didn't work:
txt=open("test.txt")
file=txt.read()
file.split("\n")
what is the difference between "r" and .read()?
The .read() function is for reading data from a file; So the file should be in read mode and the read mode is 'r' that you asked.
So 'r'is Mode for File and .read() is a function for reading data.
read() is the actual function that does the reading of any "path-like object," returning a "file-like object" (this is due to the principle of duck typing). You can optionally pass it a parameter, which is a single character, indicating what "mode" to open the path-like object. Look at the signature for read():
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
You can see that the default mode is 'r', thus, if you do not specify a mode, it will default to 'r' anyways, so including 'r' as you did is generally redundant.
The documentation is here
The r, You can think of it as the purpose of opening a file. if you open a file with r, and then you can't do write with the handler! You should got some error as :
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IOError: File not open for writing
read() just a way which you can got your data from file handler! there are also have readline() available.
Related
How can I flush the content written to a file opened as a numeric file handle?
For illustration, one can do the following in Python:
f = open(fn, 'w')
f.write('Something')
f.flush()
On the contrary, I am missing a method when doing the following:
import os
fd = os.open(fn)
os.pwrite(fd, buffer, offset)
# How do I flush fd here?
Use os.fsync(fd). See docs for fsync.
Be careful if you do fsync on a file descriptor obtained from a python file object. In that case you need to flush the python file object first.
In Python 2.7, what is the difference between the two statements:
f = open("file_name", "r")
f = open("file_name").read()
I know both are opening a file, but is first opening the file in read mode and the latter is opening the file and then reading it?
The first will return an open file object in read mode.
The second will return the contents of an open file object in read mode.
f = open("file_name", "r")
f = open("file_name").read()
The second is same as writing f = open("file_name", 'r').read().
As per python documentation the mode is an optional parameter to open(). If it is not specified, the file is open in read mode.
The first argument is a string containing the filename. The second
argument is another string containing a few characters describing the
way in which the file will be used. mode can be 'r' when the file will
only be read, 'w' for only writing (an existing file with the same
name will be erased), and 'a' opens the file for appending; any data
written to the file is automatically added to the end. 'r+' opens the
file for both reading and writing. The mode argument is optional; 'r'
will be assumed if it’s omitted.
So I am very new at file handling within python. I want to be able to read, write and append a text file all in one go. Here is what I think the code might look like:
name = input("Which file would you like to edit/view? \n --> ")
fhand = open((name), "r", "w", "a")
fhand.read()
fhand.write("Hello")
fhand.append("World")
fhand.close()
However it returns this:
Traceback (most recent call last):
File "\\godalming.ac.uk\dfs\UserAreas\Students\142659\test\filehandle tests.py", line 2, in <module>
fhand = open((name), "r", "w", "a")
TypeError: an integer is required (got type str)
Could you please advise on what is going wrong and how I could get around this?
The 'int' being sought is the integer for buffering. You've comma-delimited your 'mode' list, which is not the proper procedure; it reads "r" properly as the mode string, but when it reaches "w" it tries to take it in as the buffering int and spits back an error.
If you want to read and write, use "w+". To append to the end of the file, use f.seek(0, 2) before writing. To reset back to the start of the file, use f.seek(0, 0). And make sure to call f.close() before you end the program.
References: open method, file.seek method
The open() function accepts two parameters (or three, but not relevant here), not a file name and a list of permissions. You seem to be looking for "r+".
The error message means that open tried to use "r" as a buffer size (that's the optional third argument) which needs to be an integer, obviously.
The fix is to replace "r", "a", "w" with "r+" so that the call to open() has exactly two arguments.
hand = open(name, "r+")
(The parentheses you had around name were superfluous, so I also took those out.)
For read and write from / to a file I would recommend:
with open(filename, "r+") as f:
data = f.read()
f.seek(0)
f.write(output)
f.truncate()
Here's how you read a file, and then write to it (overwriting any existing data), without closing and reopening.
More, for all the options you can do:
with open(filename, "ra+") as f:
...
I have a file I'm trying to open up in python with the following line:
f = open("C:/data/lastfm-dataset-360k/test_data.tsv", "r", "utf-8")
Calling this gives me the error
TypeError: an integer is required
I deleted all other code besides that one line and am still getting the error. What have I done wrong and how can I open this correctly?
From the documentation for open():
open(name[, mode[, buffering]])
[...]
The optional buffering argument specifies the file’s desired buffer
size: 0 means unbuffered, 1 means line buffered, any other positive
value means use a buffer of (approximately) that size. A negative
buffering means to use the system default, which is usually line
buffered for tty devices and fully buffered for other files. If
omitted, the system default is used.
You appear to be trying to pass open() a string describing the file encoding as the third argument instead. Don't do that.
You are using the wrong open.
>>> help(open)
Help on built-in function open in module __builtin__:
open(...)
open(name[, mode[, buffering]]) -> file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
As you can see it expects the buffering parameter which is a integer.
What you probably want is codecs.open:
open(filename, mode='rb', encoding=None, errors='strict', buffering=1)
From the help docs:
open(...)
open(file, mode='r', buffering=-1, encoding=None,
errors=None, newline=None, closefd=True) -> file object
you need encoding='utf-8'; python thinks you are passing in an argument for buffering.
The last parameter to open is the size of the buffer, not the encoding of the file.
File streams are more or less encoding-agnostic (with the exception of newline translation on files not open in binary mode), you should handle encoding elsewhere (e.g. when you get the data with a read() call, you can interpret it as utf-8 using its decode method).
This resolved my issue, ie providing an encoding(utf-8) while opening the file
with open('tomorrow.txt', mode='w', encoding='UTF-8', errors='strict', buffering=1) as file:
file.write(result)
When using this code in python:
f = open('ping.log', 'r+')
f.write("["+time.ctime()+"]"+"Status")
f.close()
My file always gets overwritten. And only has one line in it, like this:
[Fri Sep 02 16:30:56 2011]Status
Why is it getting overwritten?
It's failing because you are effectively recreating the file each time as you are overwriting the first N bytes every time. If you wrote less bytes you'd see the "old" information still there.
You need to open the file for "append"
'a' opens the file for appending
Source
r+ sets the initial file pointer to the beginning. Either seek to the end or use a mode.
Check this question. Open the file with the "a" mode:
f = open("ping.log","a")
...
http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
The first argument is a string containing the filename. The second
argument is another string containing a few characters describing the
way in which the file will be used. mode can be 'r' when the file will
only be read, 'w' for only writing (an existing file with the same
name will be erased), and 'a' opens the file for appending; any data
written to the file is automatically added to the end. 'r+' opens the
file for both reading and writing. The mode argument is optional; 'r'
will be assumed if it’s omitted.
so use
f = open('ping.log', 'a')
f.write("["+time.ctime()+"]"+"Status")
f.close()