1

I'm creating a python script which should create a new file every time it generates a new string (everytime the script is executed it prints out the string to file), and also compares this random string with a string which is a text like: THIS IS A STRING, which is located on another already created file.

For example, the string THIS IS A STRING is located on file called file.dat, and the random generated string should be written to a file, in this case I call it newfile.txt.

However, I have this code:

import string
import random

file = open('file.dat', 'r')
file=file.read()
print file

def id_generator(size=28, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
file = open("newfile.txt", "w")
file.write(id_generator())
file.close()

This code, simply reads the file.dat archive, prints it to console, then generates a random string and stores it on a file called newfile.txt, but it doesn't compares anything, so, to accomplish that I've modified the code like this:

import string
import random

file = open('bullshit.dat', 'r')
file=file.read()
print file

def id_generator(size=28, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
with open("newfile.txt", "r") as f: stored = f.readline() 
if stored == id_generator(): 
    print('success!')

Now, my problem is, that this code just reads an already created file, which is newfile.txt, I need to create a new one like the code I had before, but comparing the strings.

I tried modifying the last three lines like this:

with open("newfile.txt", "w") as f: stored = f.readline() 
if stored == id_generator(): 
    print('success!')

But it throws me:

Traceback (most recent call last):
File "string2.py", line 20, in <module>
with open("newfile.txt", "w") as f: stored = f.readline() 
IOError: File not open for reading

How can I accomplish something like the first version, but comparing the strings as the second one?

3
  • you have to open the file first Commented Oct 31, 2016 at 2:15
  • when using the open call, you are using w flag which means open for writing...see documentation ..docs.python.org/2/library/functions.html#open Commented Oct 31, 2016 at 2:18
  • Thank You, I know, but what I'm trying to do, is to create the file everytime I run the script so it can be actually random, if I read it, then it is a file somehow created before, so the random code doesn't make any sense, I don't know if my approach is correct to accomplish this though. Commented Oct 31, 2016 at 2:23

1 Answer 1

1

Solution you had "w" which can only write to the file so you have to open it as "r" which reads it.

import string
import random

file = open('test.dat', 'r')
file=file.read()
print file

def id_generator(size=28, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
with open("newfile.txt", "r") as f: stored = f.readline()
if stored == id_generator():
    print('success!')
Sign up to request clarification or add additional context in comments.

3 Comments

Thank You very much, but this file should be created, not readed, if I read it, then it is the same file created before, so, should I delete it and run the script again maybe? Just to be sure, thank You. But if I delete then it is not random though.
Deleting it and rewriting it would be pretty random.
Thanks for applying my answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.