Python redirect/write output to file? - python

I want to write the output write the output in a file, but all I got was "None" even for words with synonyms.
Note: when I am not writing in the file the output it works perfectly fine Another note: the output appears on the screen whether i am writing to a file or not, but I get "None" in the file, is theres anyway to fix it.
[im using python V2.7 Mac version]
file=open("INPUT.txt","w") #opening a file
for xxx in Diacritics:
print xxx
synsets = wn.get_synsetids_from_word(xxx) or []
for s in synsets:
file.write(str(wn._items[s].describe()))

I tried to simplify the question and rewrite your code so that its an independent test that you should be able to run and eventually modify if that was the problem with your code.
test = "Is this a real life? Is this fantasy? Caught in a test slide..."
with open('test.txt', 'w') as f:
for word in test.split():
f.write(word) # test.txt output: Isthisareallife?Isthisfantasy?Caughtinatestslide...
A side note it almost sounds like you want to append rather than truncate, but I am not sure, so take a look at this.
The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

file.write() is going to write whatever is returned by the describe() command. Because 'None' is being written, and because output always goes to the screen, the problem is that describe is writing to the screen directly (probably with print) and returning None.
You need to use some other method besides describe, or give the correct parameters to describe to have it return the strings instead of printing them, or file a bug report. (I am not familiar with that package, so I don't know which is the correct course of action.)

Related

Confusion about with statement python

I have a question regarding the use of with statements in python, as given below:
with open(fname) as f:
np.save(f,MyData)
If I'm not mistaken this opens the file fname in a secure manner, such that if an exception occurs the file is closed properly. then it writes MyData to the file. But what I would do is simply:
np.save(fname,MyData)
This would result in the same, MyData gets written to fname. I'm not sure I understand correctly why the former is better. I don't understand how this one-liner could keep the file "open" after it has ran the line. Therefore I also don't see how this could create issues when my code would crash afterwards.
Maybe this is a stupid/basic question, but I always thought that a cleaner code is a nicer code, so not having the extra with-loop seems just better to me.
numpy.save() handles the opening and closing in its code, however if you supply a file descriptor, it'll leave it open because it assumes you want to do something else with the file and if it closes the file it'll break the functionality for you.
Try this:
f = open(<file>)
f.close()
f.read() # boom
See also the hasattr(file, "write") ("file" as in descriptor or "handle" from file, buffer, or other IO) check, that checks if it's an object with a write() method and judging by that Numpy only assumes it's true.
However NumPy doesn't guarantee misusing its API e.g. if you create a custom structure that's a buffer and doesn't include write(), it'll be treated as a path and thus crash in the open() call.

Python 3. File state open or closed

Started to learn Python! Finally!
The line in question:
readMyFile=open("myFile.txt","r").read()
The line above reads the txt file into variable readMyFile. What I need to know:
How can i get the state of the myFile.txt file (if it's open or closed)? And how to close it if it's open? I know how to close it in case when I have:
readMyFile=open("myFile.txt","r")
readMyFile.close
But in the case above, variable contains text from the file, not the file object.
Thanks in advance!
When you write this:
readMyFile=open("myFile.txt","r").read()
… you can't get the state of the file, or close it, or anything else. You didn't store it, so you have no way to access it. There is no magic that lets you get back information that you threw away (otherwise, there'd be no point in throwing away information, and things like garbage collection couldn't possibly work).
The solution is to just not do that: any value that you want to use, store it in a variable (or a list or dict member, or an attribute of an instance, or whatever—just store it somewhere) and then (as you already know from your question) you can use it:
>>> myfile = open("myFile.txt","r")
>>> readMyFile = myFile.read()
>>> myFile.closed
False
>>> myFile.close()
>>> myFile.closed
True
Although in this case, there's an even better answer:
>>> with open("myFile.txt", "r") as myFile:
... readMyFile = myFile.read()
Now, myFile automatically gets closed at the end of the with statement, so you never have to worry about it.
Or, even better, you can wrap this in a function:
>>> def readfile(name):
... with open(name) as myFile:
... return myFile.read()
>>> readMyFile = readfile("myFile.txt")
… and none of your code even has to think about file objects, except the two lines of code inside readfile.
In a comment, you asked:
readMyFile=open("myFile.txt","r").read() is equal to 'with' solution in regard, that in both cases file will be closed and throwed away?
No, they're not equivalent.
The first version throws away your reference to the file object, so the file is now garbage—but you're leaving it up to Python to figure out that it's garbage and clean it up (which closes the file) for you whenever it notices and gets around to it.
The with version tells Python to clean up as soon as the with statement finishes.
As it happens, in this particular example, with CPython (the Python implementation you're probably using if you don't know which one you're using), Python will figure out that the value is garbage, and close the file, at the end of the statement, so there's no difference.
But that's not true in slightly different cases. And you can't actually know which cases it's true in without knowing a bit about the internals of how CPython works. And even then, if you run the same code in, say, PyPy instead of CPython, it will do something different. And that's why many Python programs used to mysteriously sometimes fail to write the last 300 bytes of a text file, or Python web servers would occasionally fail by running out of file handles, and so on—which is exactly the problem the with statement solves: you tell Python how long you need the value to live just by a simple indent, and it makes sure that value is cleaned up at the end.
try:
with open("myFile.txt","r") as fd:
readMyFile = fd.read()
Here you don't need to worry about closing the file. Sometimes too much files opened and not closing can cause some issues. Using a with statement Python will automatically close your file after with block.

how to print lines in a file by seeking a position from another text file in python?

This is my code for getting lines from a file by seeking a position using f.seek() method but I am getting a wrong output. it is printing from middle of the first line.
can u help me to solve this please?
f=open(r"sample_text_file","r")
last_pos=int(f.read())
f1=open(r"C:\Users\ddadi\Documents\project2\check\log_file3.log","r")
f1.seek(last_pos)
for i in f1:
print i
last_position=f1.tell()
with open('sample_text.txt', 'a') as handle:
handle.write(str(last_position))
sample_text file contains the file pointer offset which is returned by f1.tell()
If it's printing from the middle of a line that's almost certainly because your offset is wrong. You don't explain how you came by the magic number you use as an argument to seek, and without that information it's difficult to help more precisely.
One thing is, however, rather important. It's not a very good idea to use seek on a file that is open in text mode (the default in Python). Try using open(..., 'rb') and see if the process becomes a little more predictable. It sounds as though you may have got the offset by counting characters after reading in text mode, but good old Windows includes carriage return characters in text files, which are removed by the Python I/O routines before your program sees the text.

How to deal with the following symbol: ¶

I'm kinda new to Python. I need to read some text from one file (A), compare it with the text from another file (B), change a part of the earlier mentioned file and write it to the third one (C). Problem is A and B files files have unusual notation that involves this symbol "¶".
So, I managed to bypass it (ignore it) by reading (or writing) in the following way:
input = codecs.open('bla.txt', 'r', 'ascii', 'ignore');
But it's not good. I NEED to read it in precise way and compare it and write successfully.
So, content of my B file is: "Sugar=[Sugar#Butter¶Cherry]"
but when I read it, my variable has the value Sugar=[Sugar#Butter¶Cherry]
You can see, there is additional "Â"
Then my A file contains a lot of text which needs to be copied to the C file, except the certain part that follows after the above mentioned text in in B. That part needs to be changed and then written, BUT they are not the same, my program never enters the IF condition in which I am comparing the "Sugar=[Sugar#Butter¶Cherry]" form A and "Sugar=[Sugar#Butter¶Cherry]" from B.
Is there a way I can read the text so that this symbol "¶" appears as it is?
Yes.
Use the correct encoding.
input = codecs.open('bla.txt', 'r', 'UTF-8', 'ignore')

Python readline() fails when trying to read large (~ 13GB) csv file [duplicate]

This question already has answers here:
Unable to read huge (20GB) file from CPython
(2 answers)
Closed 9 years ago.
I'm a Python newbie and had a quick question regarding memory usage when reading large text files. I have a ~13GB csv I'm trying to read line-by-line following the Python documentation and more experienced Python user's advice to not use readlines() in order to avoid loading the entire file into memory.
When trying to read a line from the file I get the error below and am not sure what might be causing it. Besides this error, I also notice my PC's memory usage is excessively high. This was a little surprising since my understanding of the readline function is that it only loads a single line from the file at a time into memory.
For reference, I'm using Continuum Analytic's Anaconda distribution of Python 2.7 and PyScripter as my IDE for debugging and testing. Any help or insight is appreciated.
with open(R'C:\temp\datasets\a13GBfile.csv','r') as f:
foo = f.readline(); #<-- Err: SystemError: ..\Objects\stringobject.c:3902 bad argument to internal function
UPDATE:
Thank you all for the quick, informative and very helpful feedback, I reviewed the referenced link which is exactly the problem I was having. After applying the documented 'rU' option mode I was able to read lines from the file like normal. I didn't notice this mode mentioned in the documentation link I was referencing initially and neglected to look at the details for the open function first. Thanks again.
Unix text files end each line with \n.
Windows text files end each line with \r\n.
When you open a file in text mode, 'r', Python assumes it has the native line endings for your platform.
So, if you open a Unix text file on Windows, Python will look for \r\n sequences to split the lines. But there won't be any, so it'll treat your whole file is one giant 13-billion-character line. So that readline() call ends up trying to read the whole thing into memory.
The fix for this is to use universal newlines mode, by opening the file in mode rU. As explained in the docs for open:
supplying 'U' opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention '\r\n'.
So, instead of searching for \r\n sequences to split the lines, it looks for \r\n, or \n, or \r. And there are millions of \n. So, the problem is solved.
A different way to fix this is to use binary mode, 'rb'. In this mode, Python doesn't do any conversion at all, and assumes all lines end in \n, no matter what platform you're on.
On its own, this is pretty hacky—it means you'll end up with an extra \r on the end of every line in a Windows text file.
But it means you can pass the file on to a higher-level file reader like csv that wants binary files, so it can parse them the way it wants to. On top of magically solving this problem for you, a higher-level library will also probably make the rest of your code a lot simpler and more robust. For example, it might look something like this:
with open(R'C:\temp\datasets\a13GBfile.csv','rb') as f:
for row in csv.reader(f):
# do stuff
Now each row is automatically split on commas, except that commas that are inside quotes or escaped in the appropriate way don't count, and so on, so all you need to deal with is a list of column values.

Categories

Resources