Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":27224,"date":"2015-11-24T08:40:00","date_gmt":"2015-11-24T14:40:00","guid":{"rendered":"http:\/\/kenscourses.com\/tc101fall2015\/?guid=798ccaeca8b36e1d9e89443e9291c50f"},"modified":"2015-11-24T08:40:48","modified_gmt":"2015-11-24T14:40:48","slug":"mastery30-reading-and-writing-files","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2015\/2015\/mastery30-reading-and-writing-files\/","title":{"rendered":"Mastery30 – Reading and writing files"},"content":{"rendered":"

Reading files:<\/b>
To read a file you should know this functions: open(“filename”,”r”)<\/span><\/span> and filename.read()<\/span><\/span>
The first one is used to open a file, read it and pass that information to a variable ( if you want) or to simply work with it directly.<\/span><\/span><\/span><\/span>
The second one will just read a file, to use this function ou should have used the open function first like this:<\/span><\/span><\/span><\/span>
a = open(“filename”) <\/span><\/span>
print (a.read()) <\/span><\/span>
As you can see im not using the “r” because that means that i read it at the same time i open it, if you just want to open the file but not read it right there and use that information for later you should do something like what i did above. This has a lot of functionalities if you mix this with strings manipulation. You look for a specific word, count the lines, the total words etc. Here is an aplication for that, this documento opens and reads a file and counts how many times the word “banana” is in it (WSQ14):<\/span><\/span><\/span><\/span>
def process_file(filename):
    name = open(filename)
    content = name.read()
    list = content.split(” “)
    print (list)
    counter = 0
    for i in list:
        if i.lower() == “banana”:
            counter += 1
    print (counter)
process_file(“file.txt”)<\/span><\/span>
To be sure that this file works correctly i must have a text file named “file” and be sure that each word written in it is separated by a space. If you noticed, reading a file is reading a file is really simple. Is just taking a value from a different file and assigning it to a varible, works like an input. What matters is how you control or process that information.<\/span><\/span><\/span><\/span><\/p>\n

 Writing a file:<\/b><\/span><\/span><\/span><\/span>
To write a file you have to use the same function as before but this time with a different parameter: <\/span><\/span><\/span><\/span>open(“filename”,”w”)<\/span><\/span>
This “w” thing will thell python that you want to edit\/write the file, be careful because this will erase the content of that file and create a new file<\/span>. To write a line use fout<\/span><\/span>.write()<\/span><\/span> and use as a parameter a string (or a variable with a string), when you are done use fout.c<\/span><\/span>lose()<\/span><\/span>. It is very important that you understand that the file will star writing in the last empty line.<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span> Here is an example:<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>
 <\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>def process_file(filename):
    name = open(“filename”,”w”)<\/span><\/span><\/span>
    a = “no”
    while a == “yes”:<\/span><\/span><\/span><\/span><\/span><\/span>
       line = input(“Write here the new data”)<\/span><\/span><\/span><\/span><\/span><\/span><\/span>
       fout.write(line)<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>
       a = input(“want <\/span>to close?”)<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>
    fout.close() <\/span><\/span><\/span><\/span> <\/span><\/span><\/span><\/span>
process_file(“file.txt”)<\/span><\/span><\/span>
This function will add new strings to the “file.txt” document until the user decides to stop.<\/span><\/span> <\/span><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"

Reading files:To read a file you should know this functions: open(“filename”,”r”) and filename.read()The first one is used to open a file, read it and pass that information to a variable ( if you want) or to simply work with it directly.The second one …<\/p>\n","protected":false},"author":198,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[647,431,881],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/27224"}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/users\/198"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/comments?post=27224"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/27224\/revisions"}],"predecessor-version":[{"id":27225,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/27224\/revisions\/27225"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/media?parent=27224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/categories?post=27224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/tags?post=27224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}