0

I created a notepad text document called "connections.txt". I need to have some initial information inside it, several lines of just URLs. Each URL has it's own line. I put that in manually. Then in my program I have a function that checks if a URL is in the file:

def checkfile(string):
    datafile = file(f)
    for line in datafile:
        if string in line:
            return True
    return False

where f is declared at the beginning of the program:

f = "D:\connections.txt"

Then I tried to write to the document like this:

file = open(f, "w")
if checkfile(user) == False:
    usernames.append(user)
    file.write("\n")
    file.write(user)
file.close()

but it hasn't really been working correctly..I'm not sure what's wrong..am I doing it wrong?

I want the information in the notepad document to stay there ACROSS runs of the program. I want it to build up.

Thanks.

EDIT: I found something wrong... It needs to be file = f, not datafile = file(f) But the problem is... It clears the text document every time I rerun the program.

f = "D:\connections.txt"
usernames = []

def checkfile(string):
    file = f
    for line in file:
        if string in line:
            return True
            print "True"
    return False
    print "False"

file = open(f, "w")
user = "aasdf"
if checkfile(user) == False:
    usernames.append(user)
    file.write("\n")
    file.write(user)
file.close()
Ray
  • 2,472
  • 18
  • 22
jacob501
  • 345
  • 2
  • 5
  • 17
  • Let me run it again.. – jacob501 Jan 01 '14 at 03:23
  • 1
    lol—I've never heard someone call a plain text file a "Notepad Document" before... ^_^ Anyway, I bet it has something to do with the backslash in your file path. Take a look at [this related question](http://stackoverflow.com/questions/4297450/convert-backward-slash-to-forward-slash-in-python). – DaoWen Jan 01 '14 at 03:25
  • If you're wanting to append to the file instead of overwriting it each time, see [this related question](http://stackoverflow.com/questions/4706499/how-do-you-append-to-file-in-python). – DaoWen Jan 01 '14 at 03:29
  • wow my code is completely screwed up... – jacob501 Jan 01 '14 at 03:35
  • ok it works now but it never prints true or false!! not that I need it to..just seems weird – jacob501 Jan 01 '14 at 03:39
  • The two print statements should be before the returns – Cookyt Jan 01 '14 at 03:39

2 Answers2

0

I was working with the file command incorrectly...here is the code that works.

f = "D:\connections.txt"
usernames = []

def checkfile(string):
    datafile = file(f)
    for line in datafile:
        if string in line:
            print "True"
            return True
    print "False"
    return False 

user = "asdf"
if checkfile(user) == False:
    usernames.append(user)
    with open(f, "a") as myfile:
        myfile.write("\n")
        myfile.write(user)
jacob501
  • 345
  • 2
  • 5
  • 17
0

The code that checks for a specific URL is ok! If the problem is not erasing everything: To write to the document without erasing everything you have to use the .seek() method:

file = open("D:\connections.txt", "w")
# The .seek() method sets the cursor to the wanted position
# seek(offset, [whence]) where:
# offset = 2 is relative to the end of file
# read more here: http://docs.python.org/2/library/stdtypes.html?highlight=seek#file.seek
file.seek(2)
file.write("*The URL you want to write*")

Implemented on your code will be something like:

def checkfile(URL):
# your own function as it is...

if checkfile(URL) == False:
    file = open("D:\connections.txt", "w")
    file.seek(2)
    file.write(URL)
    file.close()
RGS
  • 964
  • 2
  • 10
  • 27