Append a file

Jason Friedman jsf80238 at gmail.com
Sat Mar 7 00:37:18 EST 2015


On Fri, Mar 6, 2015 at 2:55 PM, Jason Venneri <jv92109 at gmail.com> wrote:
> Hello, I'm using the urllib.urlretrieve command to retrieve a couple of lines of data.  I want to combine the two results into one file not two.
>
> Any suggestions?
>
> Sample
> urllib.urlretrieve('http://www.airplanes.com/data/boeing1.html','B747A.txt')
> urllib.urlretrieve('http://www.airplanes.com/data/boeing2.html','B747B.txt')
>
> I would like one file say B747C that contains the data from B747A and B747B in a file named B747C

>>> f = open("B747C.txt", "w")
>>> look_for = "Aircraft,"
>>> for line in open("B747A.txt"):
...     if look_for in line:
...         f.write(line)
...
>>> for line in open("B747B.txt"):
...     if look_for in line:
...         f.write(line)
...
>>> f.close()

Seems like you are using Python 2, you ought to consider Python 3.

The requests module
(http://docs.python-requests.org/en/latest/user/install/) makes this
kind of work easier.



More information about the Python-list mailing list