Skip to main content

Posts

Showing posts from February, 2017

Start a server in Python in a second

Just write this python -m SimpleHTTPServer 8000 and you're done. Go in the browser, type localhost:8000 in the addresses bar and you will see the file in the current directory. If there is an html file or an image file, simple click on it and you'll have the html file or the image rendered. Python 3 For python 3 is different: go in the dir where you have the html file, open cmd from there and write: python -m http.server and you've finished. Go in the browser, type in the address bar: localhost:8000 and you will see the index.html file.

Python: get data from a text the fast way

This is the fastest way to get data from an external txt file that is in the same dir of the .py file with this code. You simply assign a name to a list that will get the data opening the txt file and splitting the data by the method split(), assuming that each element of the list will be a line of the text. The text (data.txt) will contain this data data 1 data 2 data 3 data 4 data 5 The code in the py file (data.py) will read those data with this code:     >>> with open('data.txt',encoding='UTF-8') as data:      >>>     data = data.readlines()     ...     >>> print(data)     [' data 1',' data 2',' data 3',' data 4',' data 5'] so, the read() method get the content of the txt file as a strint and the split() methods returns a list from the string in which each element is a line, because of the argument passed '\n' that indicates a new line. In fact, read() returns a s...