print '\033[1;32mSOME MESSAGE\033[1;m'
It would be better to replace these with a function and function call:
def print_light_green(str):
# Unless you've seen it before, "033[1;32m" is probably meaningless. What if you want to change the colour, or don't bother colouring output if using Windows?
# We use a function here to communicate what it does through its name and make it easier to change due to being in a single place, not to mention remove repetition and 'noise' in the code.
print '033[1;32m' + str + '\033[1;m'
print_light_green("Hello, world!")
print_light_green("Isn't this much prettier?")
non_blank_count = 0
with open('URL_list.txt') as infp:
for line in infp:
if line.strip():
non_blank_count += 1
fo = open("URL_list.txt", "rw+")
Why are you opening this twice, the second in rw mode?
file = open('url_list.txt')
non_blank_lines = 0
for line in file:
if line.strip():
non_blank_lines += 1
file.seek(0, 0) # Go back to beginning of file.
Note how I've given your variables more descriptive names and grouped the blank line calculation code together to show that they're related and doing a specific task, making it easier for a programmer to skim over.
line = fo.readline()
print '\033[1;33mPinging: %s \033[1;m' % (line),
for x in range(0, URL):
os.system("ping -c 1 %s" % (line)),
line = fo.readline()
print '\033[1;33mPinging: %s \033[1;m' % (line),
What an odd way to do things. I guess you didn't use for line in file for learning purposes, but why do this in and out of the loop instead of:
for x in range(0, non_blank_lines):
line = file.readline()
continue if !file.strip() # Skip if the line is blank :P I think this is valid syntax || Perl has altered my memories.
print_light_green("Pinging: %s' % (line))
os.system("ping -c 1 %s" % (line))
file.close()
Note that I use the non_blank_lines variable instead of URL because it tells us more about what is contained in the variable, Python tends to prefer lowercase_with_underscores for most variables, and URL was non_blank_lines anyway. Also, it's never a bad thing to close files when we're done.
I don't know, is this even Python?
Batch.