Objectives

  • To get practice in using Python file objects, program arguments, and regular expressions.
  • Create a python script that will read in a text file of possible credit card numbers and attempt to categorize them to major credit card networks: American Express (Amex), Visa, Mastercard

Python Program

1. Download the text file cc_numbers_python.txt and ensure it is in the same folder you will be using for this assignment.

374957991156897
2720649587451369
412
4658497501236584046584975012365840
44010400919987200
2223405693062581
2220986578452136
P(346953216584752)Q###
2723895874513658
345911586431914
5513658978452165
45678546895123
2222222222222222
DX4#$2226458745632157****p
4654511991978
$()349856987412345349856987412345p!3&
5605630148793205
349876451326598
2720603410587520
5965845362987541
garb@gE376598457845216gaRB@ge
3497584125100365
5598658475132546
2720659845124569
4678956214875
356812345710236

2. Open the text file in Atom to familiarize yourself with its contents. Each line in the file is a possible credit card number that may or may not follow the pattern of one of the major credit card networks.

3. Create a python script named LastnameFirstname03.py.

  • Ensure that cc_numbers_python.txt and LastnameFirstname03.py are in the same folder.

4. When running this script, the user is expected to supply the name of a file to read in as a program program argument.

5. To ensure the user supplied a file name, implement a program argument check. If there is not exactly 2 program arguments, terminate the script with an appropriate error and usage message.

  • Keep in mind that the first program argument is the name of the script itself, not the file to read in.
  • See the error and usage messages in the Example Outputs on the bottom. You may adapt the error and usage messages for your own scripts.

6. Attempt to open a file object for reading using the file name from the program argument. Terminate the script with an appropriate error message if the file cannot be opened, or if the user does not have read permission on the file.

  • Do NOT hard-code the file name cc_numbers_python.txt in the script. The script should attempt to open a file handle using the file name that was entered as a program argument. The file name cc_numbers_python.txt should not appear anywhere in the script.
  • See Example Output for appropriate error messages.

7. Read through each possible credit card number in the file. Each number is on its own line, therefore read the file line by line.

  • At this point, you may want to print each line in the file as it is being read to make sure the script is actually reading the file.

8. Below are the characteristics for each network's credit card number pattern. Note that this is not an exhaustive list of each pattern, but these are the ones you will be implementing in the script.

  • American Express
    • A number that starts with 34 or 37, followed by 13 digits.
  • Visa
    • .A number that starts with 4, followed by 12 digits.
    • i.A number that starts with 4, followed by 16 digits.
  • Mastercard
    • .A number that starts with 5, followed by 1 through 6, followed by 14 digits.
    • i.A number that starts with 222, followed by 1 through 9, followed by 12 digits.
    • ii.A number that starts with 2720, followed by 12 digits.

9. For each description, create a Regex object. Some descriptions can be combined into a single regex or expressed separately. For example, American Express descriptions can be expressed in one regex pattern or two, it will be up to you how you want to express the pattern. You should have around 6 patterns total. For all regex patterns, do not allow garbage at the beginning or ending of the number. In other words, only the pattern should match.

10. As the script is reading line by line, apply each pattern to determine what kind of credit card number it is. You can use a series of if-statements or elsif. When a number matches a pattern, print the name of the network in all uppercase, followed a colon :, a space, then the number. Only print numbers that match. Numbers that do not match will not have any output.

  • Compensate for either the newline that is captured from the file or the newline that is printed using print(). This means there should not be a blank line between cc numbers. Each number is printed one after another without any blank lines between as shown in the Example Output.

11. Close all file objects when done.

12. Ensure that your code is sufficiently styled and documented.

Extra Credit

In addition to printing the numbers, also write them out to a text file named matched_cc_numbers.txt on separate lines. Have the script overwrite matched_cc_numbers.txt each time the script is run, i.e. do not append.

  • Ensure each number is written on its own line, without any blank lines between, in the same manner it is printed to the screen.
  • Output an appropriate error message if the user does not have write permission on the file.

Example Output

> python MeyerEdward03.py
Error: Expecting 2 program arguments. Found 1 instead.
usage: python MeyerEdward03.py filename

> python MeyerEdward03.py pew
Error: Cannot find file: pew

> python MeyerEdward03.py 1 2 3 4
Error: Expecting 2 program arguments. Found 5 instead.
usage: python MeyerEdward03.py filename

What the error message looks like if the user does not have read permission on the file.

> python MeyerEdward03.py cc_numbers_python.txt
Error: No read permission: cc_numbers_python.txt

> python MeyerEdward03.py cc_numbers_python.txt
AMEX: 374957991156897
MASTERCARD: 2720649587451369
VISA: 44010400919987200
MASTERCARD: 2223405693062581
AMEX: 345911586431914
MASTERCARD: 5513658978452165
MASTERCARD: 2222222222222222
VISA: 4654511991978
MASTERCARD: 5605630148793205
AMEX: 349876451326598
MASTERCARD: 2720603410587520
MASTERCARD: 2720659845124569
VISA: 4678956214875

Program done.

Rename cc_numbers_python.txt to just rm, without a file extension, and run the script.

> python MeyerEdward03.py rm
AMEX: 374957991156897
MASTERCARD: 2720649587451369
VISA: 44010400919987200
MASTERCARD: 2223405693062581
AMEX: 345911586431914
MASTERCARD: 5513658978452165
MASTERCARD: 2222222222222222
VISA: 4654511991978
MASTERCARD: 5605630148793205
AMEX: 349876451326598
MASTERCARD: 2720603410587520
MASTERCARD: 2720659845124569
VISA: 4678956214875

Program done.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.