Prints output twice when reading a file
<p>I have written the following code to write a file, convert it into integer values, save that file and then read and convert it back to the original string. However, it prints the output twice.</p>

<p>My code is</p>

<pre><code>def write():
    sentence=input('What is your Statement?:')
    name=input('Name your file:')
    sentence=sentence.lower()
    words=sentence.split(' ')
    file_register=set()
    F_output=[]
    position=[]

for i in words:
    if i not in file_register:
        F_output.append(i)
        file_register.add(i)

for x in words:
    for y in range(len(F_output)):
        if x==F_output[y]:
            position.append(y)
name+='.txt'
with open(str(name),'w') as f:
    f.write(str(len(position)) + '\n')
    for c in range(len(position)):
        f.write(str(position[c]) + '\n')
    f.write(str(len(F_output)) + '\n')
    for d in range(len(F_output)):
        f.write(str(F_output[d] + '\n'))
    f.close
    global name
    global position

def read1():
    savefile=[]
    output=('')  
    with open(name,'r') as file_open:
        num=int(file_open.readline())
        while num!=0:
            a1=file_open.readline()
            a1_split=a1.split('\n')
            position.append(int(a1_split[0]))
            num-=1
        file_integer=int(file_open.readline())
        while file_integer!=0:
            word_s=file_open.readline()
            word_split=word_s.split()
            savefile.append(word_split)
            file_integer-=1

    for n in range(len(position)):
        a=position[n]
        output+=str(savefile[a])+('')
    global output

write()
read1()
print('Your file is: '+output)
</code></pre>

<p>I have tried searching, but I cannot find an answer for it. I am fairly new to Python and any help is appreciated.</p>

<p>In <code>write()</code>, you declare <code>position</code> as global. In <code>read1()</code>, you don't declare it as global, but since you never create a local <code>position</code> variable it's the global one that gets used. So you end up populating it twice: once in <code>write()</code> then once again in <code>read1()</code>.</p>

<p>To make a long story short: dont use globals. You don't need them. </p>

<p>Also, you'd find your code way easier to read, understand and debug by  1/ using better naming, 2/ writing simple, short functions that do one single thing, works on their inputs (arguments) and return values (the same arguments should produce the same output values), and 3/ properly using python's <code>for</code> loop.</p>

<p>Here's an example of what your code could looks like following those rules:</p>

<pre><code>def parse(sentence):
    sentence=sentence.lower()
    words=sentence.split(' ')

    register=set()
    output = []
    positions = []

    for word in words:
        if word not in register:
            output.append(word)
            register.add(word)

    for word in words:
        for index in range(len(output)):
            if word == output[index]:
                positions.append(index)

    return output, positions

def write(path, output, positions):
    with open(path, 'w') as f:
        f.write(str(len(positions)) + '\n')
        for index in positions:
            f.write(str(index) + '\n')
        f.write(str(len(output)) + '\n')
        for word in output:
            f.write(word + '\n')


def read(path):
    words = []
    positions = []

    with open(path, 'r') as f:
        poscount = int(f.readline().strip())
        while poscount:
            line = f.readline().strip()
            pos = int(line)
            positions.append(pos)
            poscount -= 1

        wordcount = int(f.readline().strip())
        while wordcount:
            line = f.readline()
            word = line.strip()
            words.append(word)
            wordcount -= 1

    output = []
    for index in positions:
        word = words[index]
        output.append(word)

    return output, positions



def main():
    sentence = input('What is your Statement?:')
    path = input('Name your file:')
    if not path.endswith(".txt"):
        path +=".txt"

    source_out, source_positions = parse(sentence)
    write(path, source_out, source_positions)

    read_out, read_positions = read(path)
    print("your file is: {}".format(read_out))

if __name__ == "__main__":
    main()
</code></pre>

<p>This code is still overly complicated IMHO but at least it's mostly readable, doesn't use any global, and should be easier to test.</p>

python