It seems to be an impossible behaviour. To speak about something concrete, I ran the following code
# python 2 and 3
from __future__ import print_function
def main():
valid = str("4CE1FFC64101843801")
with open("stamps", "w") as stamps:
stamps.write(valid)
with open("stamps", "r") as stamps:
check = stamps.read()
print("valid: ", valid)
print("check: ", check)
assert(check == valid)
if __name__ == "__main__":
main()
""" my output -->
valid: 4CE1FFC64101843801
check: 4CE1FFC64101843801
"""
Does it run on your system ?
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Thanks for taking a look into this :)
The variable that comes back is in fact returned from another function. This function (called snipvalue()) returns either a value (in this case
4CE1FFC64101843801) or False.
When I run the code (I have inserted the line print valid, to print the variable: valid), I get this on screen:
... but I get this in the file:
You will notice the difference between the lines on screen and the lines in the file, they appear to remove the penultimate '0'.
Here's the code that I'm using:
try:
infile_open = open(k12txt, 'r')
except IOError:
print '\nThe file "' + pcapfile + '" does not exist, exiting..\n'
usage()
exit(1)
infile = infile_open.readlines()
infile_open.close()
stamp = open(stamps, "w")
for line in infile:
valid = snipvalue(line,size)
if valid:
# Test code: Debug
print valid
stamp.write(valid)
stamp.close()
Any ideas?
Thanks again :)
What happens if you print repr(valid) ?
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Hi Gribouillis
I get this output to screen:
...and this to the file:
If you are on windows, did you try to use the modes 'rb' and 'wb' instead of 'r' and 'w' ?
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Sorry, it's a mystery. I can't reproduce the bug on my computer, so I can't help you much. I've never heard of such behaviour before :(
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
I've just discovered something strange, in that if I change the write() line to:
stamp.write(valid + "\n")
This actually shows the correct values, but puts a space in between (which means that other parts of the program fail). I don't know if this helps at all? Here's the output (windows, note the additional line in between):
...and the same in true when run on Linux:
Add assert statements to add more control
assert type(valid) is str
assert len(valid) == 18 # or 19 (with the '\n')
also try to print valid.strip(), then \n
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Would it help if I gave you access to the whole program? It's very strange as it doesn't really make any sense...
I'm running this on Python 2.7 and will try tweaking it to run on 3.1.2 and keep you posted.
Yes if you can attach a zip with the whole program, I could try it and see if I have the same bug.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Question Answered as of 2 Years Ago by
Gribouillis