Checking IP File Validity

Python 3: Automating Your Job Tasks Superhero Level: Automate Network Tasks with Python 3
4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€91.90
List Price:  €128.67
You save:  €36.76
£78.70
List Price:  £110.19
You save:  £31.48
CA$136.14
List Price:  CA$190.60
You save:  CA$54.46
A$149.21
List Price:  A$208.90
You save:  A$59.69
S$134.58
List Price:  S$188.42
You save:  S$53.84
HK$780.18
List Price:  HK$1,092.29
You save:  HK$312.10
CHF 90.89
List Price:  CHF 127.25
You save:  CHF 36.36
NOK kr1,073.95
List Price:  NOK kr1,503.58
You save:  NOK kr429.62
DKK kr686.39
List Price:  DKK kr960.97
You save:  DKK kr274.58
NZ$162.99
List Price:  NZ$228.20
You save:  NZ$65.20
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,707.96
List Price:  ৳16,391.62
You save:  ৳4,683.65
₹8,329.22
List Price:  ₹11,661.24
You save:  ₹3,332.02
RM468.70
List Price:  RM656.20
You save:  RM187.50
₦146,985.30
List Price:  ₦205,785.30
You save:  ₦58,800
₨27,706.17
List Price:  ₨38,789.74
You save:  ₨11,083.57
฿3,624.43
List Price:  ฿5,074.35
You save:  ฿1,449.91
₺3,224.01
List Price:  ₺4,513.75
You save:  ₺1,289.73
B$510.40
List Price:  B$714.59
You save:  B$204.18
R1,807.25
List Price:  R2,530.23
You save:  R722.97
Лв179.86
List Price:  Лв251.81
You save:  Лв71.95
₩135,425.45
List Price:  ₩189,601.05
You save:  ₩54,175.60
₪370.41
List Price:  ₪518.60
You save:  ₪148.18
₱5,770.97
List Price:  ₱8,079.59
You save:  ₱2,308.62
¥15,565.94
List Price:  ¥21,792.94
You save:  ¥6,227
MX$1,660.69
List Price:  MX$2,325.03
You save:  MX$664.34
QR362.75
List Price:  QR507.87
You save:  QR145.11
P1,354.73
List Price:  P1,896.68
You save:  P541.94
KSh13,098.69
List Price:  KSh18,338.69
You save:  KSh5,240
E£4,690.53
List Price:  E£6,566.93
You save:  E£1,876.40
ብር5,727.42
List Price:  ብር8,018.62
You save:  ብር2,291.20
Kz84,733.22
List Price:  Kz118,629.90
You save:  Kz33,896.68
CLP$89,440.82
List Price:  CLP$125,220.73
You save:  CLP$35,779.90
CN¥722.24
List Price:  CN¥1,011.17
You save:  CN¥288.92
RD$5,826.59
List Price:  RD$8,157.46
You save:  RD$2,330.87
DA13,436.03
List Price:  DA18,810.98
You save:  DA5,374.95
FJ$222.79
List Price:  FJ$311.92
You save:  FJ$89.12
Q776.81
List Price:  Q1,087.56
You save:  Q310.75
GY$20,917.88
List Price:  GY$29,285.87
You save:  GY$8,367.99
ISK kr13,827.61
List Price:  ISK kr19,359.21
You save:  ISK kr5,531.60
DH990.33
List Price:  DH1,386.51
You save:  DH396.17
L1,760
List Price:  L2,464.07
You save:  L704.07
ден5,654.52
List Price:  ден7,916.55
You save:  ден2,262.03
MOP$803.51
List Price:  MOP$1,124.95
You save:  MOP$321.43
N$1,819.78
List Price:  N$2,547.77
You save:  N$727.98
C$3,674.63
List Price:  C$5,144.63
You save:  C$1,470
रु13,329.38
List Price:  रु18,661.67
You save:  रु5,332.28
S/371.45
List Price:  S/520.05
You save:  S/148.59
K388.01
List Price:  K543.23
You save:  K155.22
SAR374.96
List Price:  SAR524.96
You save:  SAR150
ZK2,538.86
List Price:  ZK3,554.51
You save:  ZK1,015.64
L457.57
List Price:  L640.62
You save:  L183.04
Kč2,271.57
List Price:  Kč3,180.29
You save:  Kč908.72
Ft35,609.88
List Price:  Ft49,855.26
You save:  Ft14,245.38
SEK kr1,071.59
List Price:  SEK kr1,500.27
You save:  SEK kr428.67
ARS$88,666.13
List Price:  ARS$124,136.13
You save:  ARS$35,470
Bs690.83
List Price:  Bs967.19
You save:  Bs276.36
COP$380,678.73
List Price:  COP$532,965.46
You save:  COP$152,286.72
₡51,169.76
List Price:  ₡71,639.71
You save:  ₡20,469.95
L2,462.78
List Price:  L3,448
You save:  L985.21
₲749,130.70
List Price:  ₲1,048,812.96
You save:  ₲299,682.25
$U3,853.49
List Price:  $U5,395.05
You save:  $U1,541.55
zł392.01
List Price:  zł548.83
You save:  zł156.82
Already have an account? Log In

Transcript

Welcome in this lecture, we are going to discuss the first custom Python module for our application IP file valid dot p y. Remember, you can find the code for each of these modules after every video that follows. However, writing the code yourself while watching the videos would greatly benefit you. So let's have a look at the code for this module. There are only 30 lines of code within this module, including comments, which are very useful in the long term. So this is a pretty short module.

And honestly, most of the modules in this section are quite short so that you can understand and digest each of them better. First of all, as always, we import the necessary modules, which are Python built in modules. Although we are now building our own application, we do need to use some of pythons default modules to perform various tasks. In the case of this script, we will use the system And OS dot path modules. Next we are defining a function called IP file valid. This function will check whether the file that the user specifies at the prompt is indeed present on the local file system.

And speaking of the user, the first thing we need to do is to ask him for a file name and the full path to that file. What better way to do that than to use the input function right. So using an explicit message, we prompt the user to enter the name of the file holding the IP addresses of the Arista switches. Note that the user must input the full path to the file, including the extension dot txt in this case. Otherwise, if the program does not exist, or is not found, the program should ask the user to try again and then should quit the execution. By the way, I have already created my file called IP dot txt, containing the IP addresses of our Arista switches, with each IP on a separate line.

Pause the video for a moment and create your own file. Next after the user enters the filename and full path, we will save that string using a variable that I named IP file. At this point, we have the user input saved, and we're ready to search for the file on the local file system. To do that, we're going to use a method from within the OS dot path module, especially designed to check if a file exists or not, at the end of the path specified as an argument in between parentheses. So using an if else code block, if OS dot path dot exe file returns true for the specified file, then the file exists and the message is printed out to the screen. Otherwise, the program will display another message to the user and quit.

To quit the execution of the program. You can just use the exit method within the SIS module and the execution stops right there. Next, assuming the file has been found and validated, we open that file using, of course, the open function and the default our file access mode for reading the file to be super cautious. We also use the seek method to make sure we read the file from the very beginning and not skip any IP addresses. Then we simply apply the read lines method right here to the file in order to obtain a list where each element is a line in the file, in our case, an IP address. After extracting all the IP addresses in the file, we save the list using a variable called IP list.

And to avoid any issues, we also close the file using the close method. Finally, the last thing to do is to return the list of IP addresses using the return statement, because we will later iterate over this list when integrating all the modules inside the final application code. So again, I advise you to write this code on your own and even insert additional comments if you find it necessary. To strengthen the meaning of each line of code in this module, I'll see you in the next lecture to dissect another one of our custom modules.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.