AtBS 8: Reading and Writing Files Flashcards
What does cwd stand for?
Current Working Directory
How to return the file path for the current working directory in the interactive shell?
os.getcwd()
How to change the current working directory in the interactive shell?
os.chdir( NEW FILE PATH)
How to create a new folder?
Example
os.makedirs (FILEPATH)
import os
os.makedirs(‘C:\delicious\walnut\waffles’)
How to open a file example?
helloFile = open (‘C:\TestFolder\HelloWorld.txt’)
type hello file on the next window
How to read a file example?
helloContent = helloFile.read()
type helloContent in on next line
How to read a file by separate lines?
helloContent = helloFile .readlines()
How to write to a file?
When you open a file specify write with a “w” after the text file you want to open
ex.
baconFile = open (‘bacon.txt.’,’w’)
How to append to a file?
When you open a file specify append with a “a” after the text file you want to open
ex.
baconFile = open (‘bacon.txt’,’a’)
How to save variables in Python?
Use the shelve module.
pg 184
Difference between absolute and relative paths to files?
Absolute always begins with the root folder
Relative path only begins with the current working directory