import os

os.listdir(os.path.abspath('files'))

"""
Explanation: Lesson 31:
Reading and Writing Plaintext Files
Plaintext files contain basic text characters, and no other information. Scripts are typically text.
Binary files are any other kind of file, and are usually designed to be open by specific programs.
End of explanation
"""


dictionaryFile = open(os.path.abspath('files/dictionary.txt')) # Open the file

print(dictionaryFile.read()) # Read the file
print(dictionaryFile.readline()) # Read a line in the file until newline or EOF

content = dictionaryFile.read() # Store the content in a variable

"""
Explanation: The open() function opens a given filename as an object in 'read mode'.
It can than have methods called on it, like .read() and .readline(). 
Read mode means that files can only be read in this stage; not re-written/appended.
Files must be closed with .close() to save changes.
End of explanation
"""


helloFile = open(os.path.abspath('files/helloworld.txt'), 'w') # Create the file in writeable mode.

helloFile.write('Hello World!') # Writes to file, and returns bytes used exactly as is (no newline)

helloFile.close() # Closes/saves files

helloFile = open(os.path.abspath('files/helloworld.txt'), 'r') # Open the file in read mode

print(helloFile.read()) # Print file contents

helloFile = open(os.path.abspath('files/helloworld.txt'), 'w') # Open file for rewriting

helloFile.write('Hello Another World!') # Writes to file, and returns bytes used exactly as is (no newline)

helloFile.close() # Closes/saves files

helloFile = open(os.path.abspath('files/helloworld.txt'), 'r') # Open the file in read mode

print(helloFile.read()) # Final file contents


"""
Explanation: To open a file in write mode, pass the w parameter to the open() function.
This rewrites the entire file, and can also create files.
End of explanation
"""


os.getcwd()

"""
Explanation: Unless otherwise specified, all new files are created in the current working directory.
End of explanation
"""


baconFile = open(os.path.abspath('files/bacon.txt'), 'w') # Create file in write mode

baconFile.write('Bacon is not a vegetable.') # Write string to file

baconFile.close() # Save/close file

baconFile = open(os.path.abspath('files/bacon.txt'), 'r') # Reopen file in read mode

print(baconFile.read()) # Read the current file

baconFile = open(os.path.abspath('files/bacon.txt'), 'a') # Reopen file in append mode

baconFile.write('\nBacon is delicious.') # Write string to file

baconFile.close() # Save/close file

baconFile = open(os.path.abspath('files/bacon.txt'), 'r') # Reopen file in read mode

print(baconFile.read()) # Read the appended file

"""
Explanation: To append a file instead of overwriting it, use append mode by passing the a parameter to the open() function.
End of explanation
"""


# Define base directory
defaultpath = os.path.expanduser('~/Dropbox/learn/books/Python/AutomateTheBoringStuffWithPython')

#Change directory to files directory if set in default 
if (os.getcwd() == defaultpath):
       os.chdir('/files')   
else:
    os.chdir(defaultpath + '/files')
    
print(os.getcwd())

import shelve # import the shelve module

shelfFile = shelve.open('mycatdata')

# Create a shelf file that works like a dictionary

shelfFile['cats'] = ['Zophie', 'Pooka', 'Simon', 'Fat-tail', 'Cleo']

shelfFile.close()

"""
Explanation: To handle more data types than strings, the shelve module is often used:
End of explanation
"""


shelfFile = shelve.open('mycatdata') # Open shelve file

list(shelfFile.keys())

list(shelfFile.values())


"""
Explanation: shelve files work like dictionaries, but are stored in binary files, which cannot be easily understood in text editors
End of explanation
"""


