Fix Python – Why can’t I call read() twice on an open file?

For an exercise I’m doing, I’m trying to read the contents of a given file twice using the read() method. Strangely, when I call it the second time, it doesn’t seem to return the file content as a string?
Here’s the code
f = f.open()

# get the year
match = re.search(r’Popularity in (\d+)’, f.read())

if match:
print match.group(1)

# get all th….

Fix Python – Difference between `open` and `io.BytesIO` in binary streams

I’m learning about working with streams in Python and I noticed that the IO docs say the following:

The easiest way to create a binary stream is with open() with ‘b’ in the mode string:
f = open(“myfile.jpg”, “rb”)
In-memory binary streams are also available as BytesIO objects:
f = io.BytesIO(b”some initial binary data: \x00\x01″)

What is the di….

Fix Python – Editing specific line in text file in Python

Let’s say I have a text file containing:
Dan
Warrior
500
1
0

Is there a way I can edit a specific line in that text file? Right now I have this:
#!/usr/bin/env python
import io

myfile = open(‘stats.txt’, ‘r’)
dan = myfile.readline()
print dan
print “Your name: ” + dan.split(‘\n’)[0]

try:
myfile = open(‘stats.txt’, ‘a’)
myfile.writelines….

Fix Python – How to write to a CSV line by line?

I have data which is being accessed via http request and is sent back by the server in a comma separated format, I have the following code :
site= ‘www.example.com’
hdr = {‘User-Agent’: ‘Mozilla/5.0’}
req = urllib2.Request(site,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
soup = soup.get_text()
text=str(soup)

The content of….

Fix Python – ValueError : I/O operation on closed file

import csv

with open(‘v.csv’, ‘w’) as csvfile:
cwriter = csv.writer(csvfile, delimiter=’ ‘, quotechar=’|’, quoting=csv.QUOTE_MINIMAL)

for w, c in p.items():
cwriter.writerow(w + c)

Here, p is a dictionary, w and c both are strings.
When I try to write to the file it reports the error:
ValueError: I/O operation on closed file.

….

Fix Python – What exactly is file.flush() doing?

I found this in the Python documentation for File Objects:

flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.

So my question is: what exactly is Python’s flush doing? I thought that it forces to write data to the disk, but now I see that it doesn’t. Why?
….

Fix Python – How to open a file using the open with statement

I’m looking at how to do file input and output in Python. I’ve written the following code to read a list of names (one per line) from a file into another file while checking a name against the names in the file and appending text to the occurrences in the file. The code works. Could it be done better?
I’d wanted to use the with open(… statement….

Fix Python – Load data from txt with pandas

I am loading a txt file containig a mix of float and string data. I want to store them in an array where I can access each element. Now I am just doing
import pandas as pd

data = pd.read_csv(‘output_list.txt’, header = None)
print data

This is the structure of the input file: 1 0 2000.0 70.2836942112 1347.28369421 /file_address.txt.
Now the da….

Fix Python – Confused by python file mode “w+”

From the doc,

Modes ‘r+’, ‘w+’ and ‘a+’ open the file for updating (note that ‘w+’ truncates the file). Append ‘b’ to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the ‘b’ has no effect.

and here

w+ : Opens a file for both writing and ….