[Tutor] Create text file (original) (raw)
Bob Gailer bgailer at alum.rpi.edu
Thu Jul 1 09:35:48 EDT 2004
- Previous message: [Tutor] Create text file
- Next message: [Tutor] Create text file
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
At 07:17 AM 7/1/2004, Bernard Lebel wrote:
Hello,
I searched througout the Python docs but can't find anything about that. Is there a way to create a text file in Python? So far I'm using a FileSystemObject (wich is an ActiveX object, running inside the Win32 module), in the form of fso.createtextfile( ) But I'd like to stay away from active objects to maximize Windows-Linux compatibility.
fileObject = file('c:\foo.txt', 'w') fileObject.write('This is line 1\n') fileObject.write('This is line 2\n') fileObject.close()
Note explicit provision of newline characters (\n) at end of each line.
There is also a writelines method that takes a sequence of strings, so, using other Python shortcuts, you could replace the above with one line of code:
file('c:\foo.txt', 'w').writelines(['This is line 1\n', 'This is line 2\n'])
Bob Gailer bgailer at alum.rpi.edu 303 442 2625 home 720 938 2625 cell
- Previous message: [Tutor] Create text file
- Next message: [Tutor] Create text file
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]