INTELLIGENT ENVIRONMENT
  • Home
  • About
  • PROJECTS
  • Contact

Python

Files-create, delete, input, output, write, rewrite and more.....

        When the code is over 350 lines and the functions are repeated, I realized that I needed some information about what was happening in the program best reflected in the event logs. In python this realized very simple, instead we can use the built-in object "file", "file=open('file name.txt ', 'what you do')"
To open a text file, use:
file = open("l_name.txt", "r")
To read a text file, use:
file = open("l_name.txt", "r")
print file.read()
To read one line at a time,
file = open("l_name.txt", "r")
print file.readline()
To read a list of lines use:
file = open("l_name.txt", "r")
print file.readlines()
To write to a file, use:
file = open("l_name.txt", "w")
write("Hello World")
file.close()
To write to a file, use:
file = open("l_name.txt", "w")
lines_of_text = ["1 line of text", "2 line of text", "3 line"]
file.writelines(lines_of_text)
file.close()
To append to file, use:
file = open("l_name.txt", "a")
write("Intelligent environment")
file.close()
To close a file, use
file = open("l_name.txt", "r")
print file.read()
file.close()

About "r", "w", "x", "a" files modes you can find here.. records are made file.write('{:%Y-%m-%d %H:%M:%S} text what you want write'.format(datetime.datetime.now())+ "\n")

Source look like

log_file=open('mirror.log','a')
log_file.write('{:%Y-%m-%d %H:%M:%S} Program start'.format(datetime.datetime.now())+ "\n")


every time when I want get information about program status or function loop write code sample with same actual information
try:

        page_table = requests.get(url)
        if page_table.status_code == 200:
            p_content = BeautifulSoup(page_table.content, 'html.parser')
            log_file.write('{:%Y-%m-%d %H:%M:%S} web page status'.format(datetime.datetime.now()), page_table.status_code)


You can change log_file.write to print function as result have messages about status. Log_file write, all messages will be put in file print put messages command line mode. 


So, in mirror.py program is a place where you need it manipulate date and rewrite text after formatting line. First need get data. Whereas i use Beautiful soup (if you remember its web scrap library) and scrape data from web table, get data look this:
for i in tbody.findAll('tr'):
        cell = i.findAll('td')

        day = cell[1].find(text=True)
        desc = cell[2].text.strip()
        hl = cell[3].text.strip()
        precip = cell[4].text.strip()
        wind = cell[5].text.strip()
        hum = cell[6].text.strip()


After that, scraped data format to lines and write to file, write log and close file

data = open('weather_data.csv', 'a')
format_lines = (day + "," + desc + "," + hl + "," + precip + "," + wind + "," + hum + "\n")
data.write(format_lines)
print('{:%Y-%m-%d %H:%M:%S} web page table put to file weather_data.csv'.format(datetime.datetime.now()))
data.close()


When all needs data put in file, second function get data from file format new table and put all in GUI or tkinter in python.


 with open("weather_data.csv") as file:
        reader = csv.reader(file)
        r = 0
        for col in reader:
            c = 0
            for row in col:
                if row != row1:
                    row1 = row

                lbl = Label(frame, fg=w, bg=b, text=row, height=1, width=12, borderwidth=4, font=(fontes, 20))
                lbl.grid(row=r, column=c)

                c += 1
            r += 1
    table_frame.after(900000, weather_5)
    log_file.write('{:%Y-%m-%d %H:%M:%S} five day weather format table function threading loop'.format(datetime.datetime.now())+ "\n")  


Powered by Create your own unique website with customizable templates.
  • Home
  • About
  • PROJECTS
  • Contact