Thread: saving files

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    1
    Rep Power
    0

    saving files


    I have a simple question...I think..
    I need to go through my current directory, finding files who have their file name IN the actual file somewhere. And the list of files who do, need to be stored in another file given as the scripts argument. I just need to know how to save output to a file (that file specified on the command line). Here is what I have, but the

    #!/usr/bin/python
    import os,sys,string

    file_lst = os.listdir(".")
    print file_lst

    infile=open(sys.argv[1])
    if infile is None:
    print 'Error'

    for file in file_lst:
    if os.path.isfile(file):
    fd=open(file,'r')
    str=fd.read()
    if string.find(str,file)>-1
    print file
    infile=file.save(file)

    the last line is where it doesn't work. I can print these to screen but not save them to a file
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep Power
    0
    HI jessrs0290,
    first of all remember when you post a code snippet to put it in a forum "code" block so the indentation remain correct.
    About your problem:
    in the last line the var file contain the file name retrieved from the list with the for..in.. loop and not a file object, it can't have a method save().
    If I understand what your code have to do well , my solution should be like this:

    Code:
    #!/usr/bin/python
    import os,sys,string
    
    file_lst = os.listdir(".") 
    print file_lst
    
    infile=open(sys.argv[1])
    if infile is None:
        print 'Error'
    
    for file in file_lst:
        if os.path.isfile(file):
            fd=open(file,'r')
            str=fd.read()
            if string.find(str,file)>-1
                print file
                #infile=file.save(file)
                infile.write(file + '\r\n')
    infile.close()
    HTH
    Marco Bettio

IMN logo majestic logo threadwatch logo seochat tools logo