[Tutor] Files and file attributes

David Porter jcm@bigskytel.com
Wed, 21 Feb 2001 16:56:56 -0700

* Pat/Bob Fisher <pbfisher@ispchannel.com>:

> I am pretty new at programming in Python, but have had experience in
> programming in other languages.  I went through the tutorial which I
> thought was fairly good, except the section on the methods of file objects
> was pretty sparse.  It seems to me that dealing with files is rather
> awkward. I have used the list representation filelist = f.readlines() and
> then used the fine list methods to manipulate the filelist, but I am having
> trouble getting a new file from my new filelist.

Could you elaborate on this?

> The g.write('string') gives me a whole lot of extraneous stuff.

What is the extraneous stuff? My guess is that you did not open g for

g = open('file', 'w')

> Another problem I am having is trying to open files in other directories.
> For example,
>    f=open('C:\tmp\testfile','r+') just doesn't work for me.

Backslashes mean something special inside strings. E.g., \n is a newline and
\t is a tab (looking above, you accidentally embedded two tabs in the
filename). If you want it to really be a backslash then use two:

f = open('c:\\tmp\\testfile', 'r+')