简体   繁体   中英

Python writing on a text file

So I have an xml file, a python code, and a fileList.txt.

I have to extract the path from the xml file (it's done), and write it to a fileList.txt file. I have no problem writing it, but I would like to check in that file if the path isn't already present. I just can get through this. Here's what I wrote. I tried with a for, but it didn't work. Thanks in advance

fileList.txt: 
USM/src/

python:

for racine in rootElements.findall('racine'):
    path = racine.find('path').text 
    if path != None: 
        f_path = f_path + path + "/"  
print f_path

file = open('fileList.txt','r') 

while 1:
     ligne = file.readline()
     if(ligne == f_path):
         print("path already present")
         sys.exit(0)
     else:
         break
file.close()

file = open('fileList.txt','a') 
f_path = f_path + "\n"  
file.write(f_path)      

file.close() 

Your infinite while loop will only ever execute once; you check if the first line is a match, then if it is you exit the program entirely and if it's not you exit the loop.

Something like this will work better:

with open('fileList.txt','r+') as myfile: #file is a builtin, don't name your file 'file'
    for line in myfile:
         if line.strip() == f_path:
              print "path already present"
              break
    else:
        myfile.write(f_path)

The others are good, but have the mistake of using a file object for an iterable. To fix that, use the File.readlines() method to create a list of iterables. Here is the entire code:

for racine in rootElements.findall('racine'):
    path = racine.find('path').text 
    if path != None: 
        f_path = f_path + path + "/"  
print f_path

file = open('fileList.txt','r') 

with open('fileList.txt', 'r') as myfile:
    for line in myfile.readlines() : # here is the fix
        if line.strip() == f_path:
            print("path already present")
            sys.exit(0)

with open('fileList.txt','a') as myfile:
    f_path = f_path + "\n"  
    myfile.write(f_path)

file.readline() will include the line-terminator character, ' \\n '. You could try file.readline().strip() to remove leading and trailing whitespace.

Also, your while loop doesn't look right. It never loops more than once, because either it finds a match on the first line and invokes sys.exit(0) , or it does not and hits the break to end the loop.

A better approach instead of the while loop might be:

for line in file:
    if line.strip() == f_path:
        print("path already present")
        sys.exit(0)
file.close()

EDIT - You report getting "type object is not iterable". That generally means that the sequence in the for loop (here " file ") is not of an iterable type. In your example code, file was the name of a file you had previously opened. This is a bad name for a variable, because Python already uses file as the class for file objects. Perhaps you have changed the code to use a different name for the file variable, and not updated my example to match? Here's a complete version of your program that should work:

for racine in rootElements.findall('racine'):
    path = racine.find('path').text 
    if path != None: 
        f_path = f_path + path + "/"  
print f_path

file = open('fileList.txt','r') 

with open('fileList.txt', 'r') as myfile:
    for ligne in myfile:
        if ligne.strip() == f_path:
            print("path already present")
            sys.exit(0)

with open('fileList.txt','a') as myfile:
    f_path = f_path + "\n"  
    myfile.write(f_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM