|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > saving files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|