Reputation: 193
Here is my code :
class badCmds:
def removeCmds(self):
fname = input("Enter bad command list filename")
badCmdList = open(fname, "r")
fname2 = input("Enter your script's filename")
myScript = open(fname2, "r")
commentedCmd = ""
for badCmd in badCmdList:
#myCmd = "#Script Generated by HP SSO Administrator";
for myCmd in myScript:
if badCmd in myCmd:
commentedCmd = "#"+myCmd
print(commentedCmd)
badCmds().removeCmds()
Basically, I have a script I am pulling from a certain program to put into another program. However, there is a list of disallowed commands when putting it into the second program, so I tried to write this little code to comment out ("#") any of the disallowed commands. I'll worry about printing the list correctly later, for now all I want are the hashed out commands.
When I run this code, it hits the first loop the first time, spins through the second loop until it hits the end of the file, then spins through the first loop, minus the first value, never again entering the second for loop. I don't really get how For counters work in Python; are "badCmd" and "myCmd" strings or ints? They print like strings but count like ints. You can see my commented out line, where I try to restart the counter on the second For loop by giving myCmd a string value, which happens to be the first line in the second file. Clearly doesn't work.
thanks for any help.
Upvotes: 1
Views: 2332
Reputation: 16625
That code is a bit too loopy. You should store bad command list in memory so you do not need to read it from file again and again. And you should leverage list comprehensions so you do not need to reset any counters:
def removeCmds():
fname = input("Enter bad command list filename")
fname2 = input("Enter your script's filename")
# load files to lists
badCmdList = [line.strip() for line in open(fname, "r") if line.strip()]
myScript = [line.rstrip() for line in open(fname2, "r")]
# print filtered commands
for cmd in myScript:
# here comes the magic:
if any(bad in cmd for bad in badCmdList)
print("#"+cmd)
Upvotes: 0
Reputation: 23624
Using in
on the file will get one line at a time until it hits the end of file. When trying to use in
on the file again, it will fail because you've already hit the end of the file.
This means you have two options. Save the file in a list, or open it every time.
Also when opening files in python its best to use the with
keyword as it will handle file errors and close the file for you.
class badCmds:
def removeCmds(self):
badCmdFileName = input("Enter bad command list filename")
myScriptFileName = input("Enter your script's filename")
with open(badCmdFileName, "r") as badCmdFile:
for badCmd in badCmdFile:
with open(myScriptFileName, "r") as myScriptFile:
for myCmd in myScriptFile:
if badCmd in myCmd:
print("#" + myCmd)
badCmds().removeCmds()
Upvotes: 1
Reputation: 77347
myScript is an open file - after running through it the first time, the file pointer is at the end of the file and future iterations have nothing else to return. You could use myScript.seek(0,0)
to reset the file pointer, but this is a good use-case for reading the entire file into a list.
class badCmds:
def removeCmds(self):
fname = input("Enter bad command list filename")
badCmdList = open(fname)
fname2 = input("Enter your script's filename")
myScript = open(fname2).readlines()
commentedCmd = ""
for badCmd in badCmdList:
#myCmd = "#Script Generated by HP SSO Administrator";
for myCmd in myScript:
if badCmd in myCmd:
commentedCmd = "#"+myCmd
print(commentedCmd)
Upvotes: 1
Reputation: 26
Iterators take the type of the data being placed in them, defined as their element in the set that is being iterated in the loop.
So if you have some sort of list like [1, "Hello", 2, "Goodbye"] and iterate through it, the iterator will be an int, then a str, then an int, then a str, respectively.
If it's going to be an issue, you can always just force everything to be a string by converting your iterator inside the for loop. You probably don't even need a try/except for that.
Upvotes: 0