This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write mode creates a new file or overwrites the existing content of the file. | |
# Write mode will _always_ destroy the existing contents of a file. | |
try: | |
# This will create a new file or **overwrite an existing file**. | |
f = open("file.txt", "w") | |
try: | |
f.write('blah') # Write a string to a file | |
f.writelines(lines) # Write a sequence of strings to a file | |
finally: | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The built-in function `open` opens a file and returns a file object. | |
# Read mode opens a file for reading only. | |
try: | |
f = open("file.txt", "r") | |
try: | |
# Read the entire contents of a file at once. | |
string = f.read() | |
# OR iterate over the file line-by-line: | |
for line in f: | |
# Do something with line. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
class MyClass { | |
public static void main(String[] args) { | |
JFrame frame = new JFrame(); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
Container contentPane = frame.getContentPane(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Recursively walk a directory with os.walk.""" | |
import os | |
base_dir = '/home/seanh' # The directory to walk | |
for root, dirs, files in os.walk(base_dir): | |
print 'root: ',root | |
print ' dirs:' | |
for d in dirs: | |
print ' ',d |
NewerOlder