Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 1st, 2003, 09:30 AM
jessrs0290 jessrs0290 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 1 jessrs0290 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old August 1st, 2003, 11:20 AM
Tio Tio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 Tio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > saving files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap