Skip to main content
deleted 43 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 480

Ignoring the copy&paste indentation error, thereThere are quite a few possible improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All non-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

The easiest fix for this would be using this:

if "user" in lines and "password" in lines:
    # Stuff

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

Ignoring the copy&paste indentation error, there are quite a few possible improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All non-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

The easiest fix for this would be using this:

if "user" in lines and "password" in lines:
    # Stuff

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

There are quite a few possible improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All non-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

The easiest fix for this would be using this:

if "user" in lines and "password" in lines:
    # Stuff

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

added 117 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

Ignoring the copy&paste indentation error, there are quite a few possible improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All non-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

The easiest fix for this would be using this:

if "user" in lines and "password" in lines:
    # Stuff

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

Ignoring the copy&paste indentation error, there are quite a few possible improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All non-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

Ignoring the copy&paste indentation error, there are quite a few possible improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All non-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

The easiest fix for this would be using this:

if "user" in lines and "password" in lines:
    # Stuff

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

added 213 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

Ignoring the copy&paste indentation error, there a reare quite a few possibilepossible improvements.

You should open files with the withwith keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All emptynon-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswithstr.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehensionlist-comprehension and used the fact that you can directly iterate over a file.

Ignoring the copy&paste indentation error, there a re quite a few possibile improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

Ignoring the copy&paste indentation error, there are quite a few possible improvements.

You should open files with the with keyword, this way they get closed no matter what (you don't even close your file).

if 'user' and 'password' in lines: does not mean what you think it does. All non-empty strings are truthy in Python, so if "user" is always True. So you only really check that the line contains "password". Fortunately you don't have any lines containing "password", but not "users", so this bug has not yet become obvious.

In summary, I would re-write your code like this:

with open("haproxyfile.txt") as f:
    users = [line.split()[1] for line in f 
             if line.strip().startswith("user") and "password" in line]

print users

Note that I omitted the "r" parameter to open, because read-only is the default mode. I also used str.startswith to check if it starts with a string. The strip is probably needed, to get rid of the whitespace at the beginning of your lines. If that is a copy&paste error as well, omit it.

I also wrote it as a list-comprehension and used the fact that you can directly iterate over a file.

Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading