lyrics = "Imagine all the people, \nliving life in peace... \n\tJohn Lennon"
print(lyrics)

our_file = open("imagine_lyrics.txt","w")

"""
Explanation: Reading and Writing files
There is a built ing function in Python which is used for opening files. The function is called open() and takes two arguments:

the filename (required argument),
the opening mode (optional argument).

Opening mode tells Python for what reason to open the file. The possible values for that argument are:

"r" - Read mode which is used when the file is only being read, this is the default value of the argument, if it is not explicitly declared,
"w" - Write mode which is used to edit and write new information to the file (any existing files with the same name will be erased when this mode is activated),
"a" - Appending mode, which is used to add new data to the end of the file; that is new information is automatically amended to the end,
"r+" - Special read and write mode, which is used to handle both actions when working with a file

The open() function works closely with the close() function, which tells Python to close the opened file.
Let's, for example, write the lyrics of John Lennong's "Imagine" song to a new text file. Then that file will be used for reading purposes.
Part 1: Writing to txt files
End of explanation
"""


our_file.write("Imagine by John Lennon")

our_file.write(lyrics)

"""
Explanation: So now a new text file titled "imagine_lyrics" was created for writing purposes (please take a look to the file dimension .txt which was mandatory to include). We can now use the write() function to write different text to our new file. We will write there the lyrics.
End of explanation
"""


our_file.close()

"""
Explanation: As mentioned above, once the actions with the opened file are complete, you may already close it.
End of explanation
"""


lyrics_imported = open("imagine_lyrics.txt","r")

print(lyrics_imported)

"""
Explanation: Part 2: Reading from .txt files
Once the file already exists, we can now read it back to Python. Again, we must open it, but in read only mode this time.
End of explanation
"""


lyrics_imported.read()

print lyrics_imported.read()

"""
Explanation: As you can see above, hte file was succesfully read, but printing it gives only some info, and not the content of the file. For that reason, the read only opened file should also be read, using the built-in read() function.
End of explanation
"""


lyrics_imported.readlines()

"""
Explanation: If you give any integer as an argument to read() function, then it will return only that much very first characters of the string (e.g. read(7) would return "Imagine", while read(4) would return "Imag").
One thing you may have already noticed about read() function is that it returns one full string from the text file. Yet, you might also be interested in reading the file line-by-line. For that purpose, the readlines() function can come handy.
End of explanation
"""


lyrics_imported = open("imagine_lyrics.txt","r")

lyrics_imported.readlines()

"""
Explanation: In Jupyter notebook (this is specific to Jupyter notebook only), the opened read only file can be read only once. So In order to read it again, we have to open it once again.
End of explanation
"""


lyrics_imported = open("imagine_lyrics.txt","r")
lyrics_list = lyrics_imported.readlines()

type(lyrics_list)

"""
Explanation: As you can see, not only it read each line separately, but the output we received has the type of list. We may check it, if we save the results in a new file.
End of explanation
"""


lyrics_list[0]

lyrics_list[1]

"""
Explanation: Fine, now we are sure that the output is a list, so we can perform different actions on it
End of explanation
"""


lyrics_imported.close()

"""
Explanation: Once verything is done, we should remember to close the file down.
End of explanation
"""


with open("imagine_lyrics.txt","r") as data_imported:
    data = data_imported.readlines()

"""
Explanation: Final note: besides readlines() function there is only the singular version called readline() which returns a single line from the text file (e.h. readline(3) will return the whole content of the 3rd line only).
Part 3: automatically closing files
One of the main drawbacks of the above approach is that you shouls always remember to close the files once your actions are completed. A good shortcut is to write all content to a new variable, and close your file immediately. For that purpose a very handy statement called with exists, which closes the file itself. Let's try it out.
End of explanation
"""


print data

print data_imported

"""
Explanation: As it can be seen above, no close function was used, as the with statement automatically closes the file down. Another important thing to note, is that the data_imported varible was a temporary name used for an imported file to perform actions on. So we wrote its content to a new global variable (called data) that can be used further for analysis.
End of explanation
"""


