I have to read all the files that are inside a directory and then compare them with those of a .txt file
import os
from os import listdir
from os.path import isfile,join
mi_path = "c://python27//proyectos//"
f = open(mi_path+'datos.txt', 'r+')
b = open(mi_path+'nuevo_dato.txt', 'w+')
datos = f.read()
for(ruta, directorio, archivos) in os.walk(mi_path):
for i in archivos:
b.write(i)
print "Directorio leido"
nuevoreg = b.read()
if datos == nuevoreg:
print "NO hay un fichero nuevo"
else:
print "SI hay un fichero nuevo"
f.close()
b.close()
The problem is that when I save the list I get from reading the directory in the .txt new_dato, it is saved with a lot of encoding to larger files than there are in the directory itself.
1.txt2.txt3.txt4.txtdatos.txtdoc 2.txt
j t
j & f} t 'ç t
_ t '
When doing the comparison between datos.txt and nuevo_dato.txt it always tells me that there is new data because all of that data is stored in the txt. If I make a print i to show the list of directory data before burning them in the txt it works perfectly, the problem is when you record them.
Can someone tell me what I'm doing wrong? and another question, could you store the files contained in the directory in a list instead of a txt?
Good @FJSevilla, if exact, what I want is to read the files that are inside the directory and buy them with the names that are inside data.txt and when when doing os.walk there is a new one let me know. Inside data.txt let's say I have: doc1.txt doc2.txt doc3.txt which are the same files that I have inside the directory. The problem as I comment is that doing the write in the file new_dato to dump the return of os.walk adds a lot of things besides the name of the files, but if I make a print to show the result of os.walk in the interpret, those characters do not appear anywhere and gives me print on the screen the names of the files that are inside the directory in alphabetical order. I do not know what is the reason why, if I print it shows me the list well but if I do write to .txt and then buy both fails.