0

I'm trying to make a program in python that edits the files in the current directory and adds the code at the end of the program. I haven't finished it, but I've put the marker for the virus in my other files and started the program in order to see if the marker is in the other code and it always returns false. Unsure as to why it does this.

import os

path = ("/python34")
def infected(__file__):
    if("#@! infected by virus ;) !@#") in (__file__):
        print("True.")
    else:
        print("False.")

def selectTarget():
    os.getcwd()
    os.listdir(path)

def copyCode(__file__):
    open(__file__, 'r+')
    victimfile=open(__file__)

selectTarget()
infected(__file__)
copyCode(__file__)
Others
  • 2,876
  • 2
  • 30
  • 52
Yoast
  • 3
  • 1
  • 1
  • 2

2 Answers2

0

The only reason this doesn't raise errors is that you've selected a builtin in variable (__file__) to use as the parameter to your functions. You shouldn't use builtin names for many reasons, but here the problem is that it masks real bugs.

So, change __file__ to filename and you'll start hitting lots of bugs. For instance, its never defined. And after you define it, you'll find that your funcitons don't return anything so whatever they calculate is discarded as soon as they exit.

I think you should run through the python tutorial before taking on this more complex task! You'll save yourself a lot of confusion.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

This might work. I took the suggestions of "tdelaney" and edited your original code. Hopefully this helps, if anybody has edits to make it better, please do.

import os

filename = raw_input("File Name: ") # Type whatever your file is
path = ("/python34")
def infected(filename):
    if("#@! infected by virus ;) !@#") in (filename):
        return True
    else:
        return False

def selectTarget():
    os.getcwd()
    path_list = os.listdir(path)
    return path_list

def copyCode(filename):
    open(filename, 'r+')
    victimfile=open(filename)
    return victimfile

for i in selectTarget():
    if infected(filename) == True:
        infected_file = copyCode(filename)
        # Do something with infected file
PyGuy
  • 49
  • 3