Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Program that creates files in py format.

Hi everybody!

I am taking python course on coursera and sometimes I get some url links with python files. So I wanna create some programm which retrieves files from some web sites, creates python file and embeds all the retrieved data to the file.

import socket

url_input = input("Input a url: ")
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.pythonlearn.com', 80))
mysock.send('GET {} HTTP/1.0\n\n'.format(url_link).encode())

while True:
    retrieved_data = mysock.recv(512)
    if (len(retrieved_data) < 1):
        break

mysock.close()

So, is there any way to create python files automatically and embed all the retrieved data to it?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

A Python file is simply a regular text file named with a .py extension. Some also place a shebang as the first line in the file, such as:

#!/bin/python