import os import csv import datetime dt = datetime.datetime.now() print("******************************************************************************************************************") print("The Time is {}/{}/{} {}:{}:{}.".format(dt.month,dt.day,dt.year,dt.hour,dt.minute,dt.second)) print("******************************************************************************************************************") #theFile = open(r"c:\users\gjackson\myFile.txt",'r') myFile = open(r"c:\users\gjackson\foo.txt",'wt') print myFile.name + " is now opened up." myFile.write("This is a bunch of File text.") myFile.write("This is more text with a Carriage Return.\n") myFile.write("This will have a CR") myFile.write("This will have a CR") myFile.write("This will have a CR") myFile.write("This will have a CR") myFile.write("This will have a CR") myFile.close() print myFile.name + " is now closed" FileHandle = open("foo.txt",'rt') print("test This\n") print(FileHandle.read()) print("tested") fo = open("foo.txt", "r+") fo.seek(0,os.SEEK_END) intLength = fo.tell() fo.seek(0,0) strContent = fo.read(intLength); print strContent fo.close() #CSV Files print("\n\n") myList = [["Bill Smith","503-987-0099","12345","11/15/1972"], ["Jill Jones","503-520-1234","88888","11/15/1972"], ["Jeb Nork","503-123-4444","55555","11/15/1972"], ["Ken Halligan","503-456-3333","90554","11/15/1972"], ["Angela Comstock","503-789-2222","32877","11/15/1972"], ["Tara Millan","503-999-1111","90110","11/15/1972"], ["Deanne Lang","503-224-0000","00000","11/15/1972"] ] with open('Employees.csv', 'wt') as b: a = csv.writer(b) a.writerows(myList) #Now Open the CSV File and Process it with open('Employees.csv', 'rt') as theFile: data = theFile.read() print(data) print("\n\n") print("DONE") Employees = [] f = open("Employees.csv") csv_f = csv.reader(f) for aRow in csv_f: Employees.append(aRow) #print aRow print(Employees) #Fixed Width Files fmt="%4s%10s%10s" >>> fmt % (1,"ONE",2) ' 1 ONE 2' a_file = open(data_file) data = a_file.readlines() for a_line in data: record_type = a_line[:3] parcel_num = a_line[3:11] the_rest = a_line[11:] print record_type, parcel_num, the_rest #Binary Files #f = open("myfile", "rb") #try: # byte = f.read(1) # while byte != "": # # Do stuff with byte. # byte = f.read(1) #finally: # f.close()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question