Reputation: 965
I feed a list of files that need to be backed up in a function that tars them.
The list is a result of a comparison of two text files that contain checksums.
How the files are made:
hash = hashlib.md5(path + '/' + file).hexdigest()
f.write('{} - {}'.format(hash, path + '/' + file) + '\n')
How they are compared:
with open(tmpfile, 'r') as f1:
with open(storagefile, 'r') as f2:
diff = set(f1).difference(f2)
I get the following error when tarring:
[Errno 2] No such file or directory: '/XXX/XXX/XXXX/XXXX/Trash/files/hihi\n'
Notice the '
and \n
in the filename
If I print the path before tarring there is not trace of the '
and \n
/XXX/XXX/XXXX/XXXX/Trash/files/hihi
Does someone have an idea why this is happening or how to fix this?
Maybe I should use a stream writer instead of having to rely on \n
Upvotes: 0
Views: 51
Reputation: 114911
When the file is read with set(f1)
, the lines read from the file include the newlines (similar to f1.readlines()).
For example:
[5]: !cat foo.txt
foo
bar
baz
In [6]: with open('foo.txt', 'r') as f:
...: s = set(f)
...:
In [7]: s
Out[7]: {'bar\n', 'baz\n', 'foo\n'}
There are many ways you could fix this. For example, use:
diff = {name.rstrip('\n') for name in set(f1).difference(f2)}
That should work fine if the files are always created using the code shown in the question. If eventually you might read files created elsewhere, you should be safe and strip the newline characters before putting the lines into sets. That will avoid the potential problem of a file not having a final newline.
Upvotes: 2