Python 3 Programming Tutorial - Sys Module
Skills:
Tool Use & Function Calling80%Prompt Craft60%Prompting Basics60%Prompt Systems Engineering60%
Key Takeaways
This video tutorial covers the basics of the sys module in Python 3, including sys.argv for command line arguments, sys.stderr and sys.stdout for error and output handling, and demonstrates how to use these features to pass arguments to a Python file and retrieve their values.
Full Transcript
Hello everybody and welcome to another Python 3 tutorial video. In this video, we're going to continue on with our standard library uh modules uh minieries of our Python 3 series here. Uh the last video we're talking about OS. This video we're going to be talking about CIS or short for system. Um system is actually quite interesting. You can use this to do any sort of like system input output kind of stuff. Basically, if you're thinking more like command line arguments, stuff like that. am really interesting is the ARGV um ARV functionality. So that's mostly what I'm interested in showing you guys today, but I'll also be showing you SC out uh and error, especially the differences of those. But anyway, uh with that, let's go ahead and get started. So, first we're going to need to import SYS. And now I'm going to show you guys like SC air and SC out. Now, a lot of people ask like what is the difference of these two things? For the most part, there really is no difference. They're very similar. Um, you should just keep SE air strictly for errors and SC out for everything else. Um, really SC air is just unbuffered. And there's a couple other things that we could do S st error specifically, but for now we're just going to assume they're basically the same, but I'll show you guys them anyways. So, cis.stdair.right and we're just going to write test. And after you do SD error, um it's a good idea to go ahead and do cycdair. Flush flesh. Um yeah, so we'll say test. So we print this out and you should get output like this. So that like red error text that you're used to seeing if you've ever gotten an error, which I'm sure you have. Um and I'm going to change this to this is std air text like that. And then I'll show you guys now cy.std SC out. And then we'll say this is SC out text. And also what you're going to see is that uh well I'll show you guys in a second. So we'll save and run that and you get this. So you get this error text and then I see out text is just that blue text. Um but you see they're on the same line. So what we can do is at at the end of all of these you would kind of want to always add a new line there. That way everything continues on on the next line. So it just kind of organizes it a little bit for you guys. Uh so anyway, we'll close out of that. And now we get to the fun stuff. This is arg. So just to show you guys a basic example, we could say print uh cis.arv like this. And if we print that, we get basically the file path. And this is the path to the file I'm editing right now, which is template.py. Now that basically is just going to give you the file name. Okay. But what we can do with this is actually pretty cool. We can pass arguments into this Python file using our command line which means using any other language basically or you can use the command line yourself as well but you can use it for like batch scripts all kinds of really really cool stuff. So anyway um or really communication between you know PHP and Python or Pearl or Ruby or whatever language you're trying to use you can use this to communicate back and forth. And uh anyway, well, let's go ahead and get started. So, cy.orgv is just going to display all of the arguments. Um, so what I can do here though is we'll save this. And in fact, let me do something over here. Let's bring this. Let's just copy and paste this into this file. Save. So, I've saved that this data to this file here that's in CIS. And so we'll come over here. We have CIS. And if you hold your shift key and right click on the folder here that you're in, you can open the command line there if you're on Windows. Anyway, not sure if it works on Linux or not, but you can on Windows. And so now we've opened up this uh file that looks just like uh this. Okay. And in fact, I guess we'll leave that input output text just for kicks for now. Um, so let me see if I can make this a little bigger for you guys. Can I just change the font size? Wow. Okay, I guess we'll just stick with this for now. So in here now we can start passing arguments through here. So what we can do is I've changed the file name to um cis.py. So if I run the python cis pi like that. It's going to give us our st air text stout out text and then it's giving us our argv. And our argv is just that file name that we just ran through. So rv is basically all the arguments that you pass through Python. So when we ran Python, our first argument really was that file name cis.py, right? So that's why we get cis.py. But we can pass other things through. So we could say pythoncis.py. Um, look at that. Run that. And now we get this Python list of cy.py. And then look at that. Now, what I want to do is let's come back over uh to Python 3 cis. We'll open that up. And this is the one that we just made. So, this is the data that's in this file. And let me go ahead and make this big text, too, so it's good to see. And see, this just prints cis.orgv. But now we can do some fun stuff with this. So for example, what I would do is if you're going to start using arguments, before you start referencing the arguments, it's a good idea to go ahead and make a check for that amount of arguments. So here, let's say we're just going to have the file name and then one more argument that we're expecting. So we're going to say if cis or if len cis.orgv and what this does is just tells us the length of a list. So if you had 15 elements, len cis.orgv argv would be 15, right? So if the length of cis.orgv is greater than one, so if we have more than one argument, then what we want to do is let's go ahead and just print cy.orgv and then one. So this one, right, the first element is zero. The second element is called one. It has an ID of one. So we'll save that and we'll come back over here and we're going to do up arrow. Look at that. And now what it's going to do is you see how it printed out for us you know the SC error and out and then it prints us the list but then we can we can see here that we were able to actually reference look at that specifically because that was one of our um argument variables. So now just to keep things clean, let's comment all of this stuff out and this out. And now instead of printing that uh what we're going to say is print um in cy.orgv and actually let's do float just say cis.orgv plus five. Can we get away with this? So we'll save that. Come back over here and let's do instead of look at that let's say five. And so we can see that this must be a string or a number as the argument that we've passed through. Um, and what we did here is we have to reference that. So let's try this one more time. And now you get 10.0. Okay. So the reason why that had failed at first is we didn't put the the ID. So we're saying print float the float version. Now we could change this to int and then it wouldn't be a float. But just in case like say you wanted to do uh 5.67 or something like that, then it was 10.67 six seven because we added five. So we can start manipulating the values that come in uh to Python. And so we can use this to pass values from anything, right? You can do it either from the the command line itself. You can do um like say you're you're writing something in PHP like for example I use this kind of stuff to communicate between PHP and Python because I hate web design. I just hate it. I can't do it. And um you say that you have a Django series but I hate it. Anyway um I don't like doing it. And so most of the websites that I make for myself I just use WordPress. and WordPress is PHP and but like for backend stuff I I use Python, right? Because Python is the language I know best. I don't really like PHP that much and I like Python. So, um what I'll do is I'll use this stuff to communicate between PHP and Python which is otherwise kind of difficult to do but you can use this to use Python. You can make a function and you can make that function argument um or the function parameter rather assist.argument, argument, right? So, for example, you could say import cis and now we can come back up here and we could, you know, define main and then we can call this cis.org v one um like that. And then we could say print.orgv one. That's just our function for example. And then we go ahead and run main and and really actually what we would what we'd probably rather do is instead of this we'll just call arg and call this arg like that main cy.org v1. We can save that and we can come over to our command line here and do python cis.org or cis.py rather 5.67. We're going to print that argument in the main loop. We did save that. Yeah. and run it. And you can see here that we've commented out everything. We made this function that uses that argument. We actually pass cy.archv through that uh function and then we get our return which is really just you know arg uh or the arc v which is the same thing we were doing but you can use it to pass through functions classes whatever you can use that. Um so you can do a lot of really really cool things uh just with cis argv. So anyway uh hopefully that was useful for some of you guys. a really interesting topic. There are a lot of hacky things you can do with this. Um, maybe more on that later, but I did just want to cover kind of the basics of that. Um, if anybody has any questions or comments, feel free to leave those in the comment section below. Also, if you have any requests, feel free to make them. Especially if you have a specific request maybe with CIS module or if you have any other uh standard library modules you definitely want to see, let me know. Uh, anyway, that's going to conclude this video. As always, thanks for watching. Thanks for all the support, subscriptions, and until next
Original Description
This video covers some of the basics to the sys module in Python 3. The sys module allows you to use stdin() and stdout(), as well as stderr()... but, most interestingly, we can utilize sys.argv(). To many this is a confusing concept, but it is pretty simple and very useful once you learn it. The idea of sys.argv is to allow you to pass arguments through to Python from the command line.
This ability acts as a bridge to the ability to communicate between Python and other languages.
Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/
Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Tool Use & Function Calling
View skill →
🎓
Tutor Explanation
DeepCamp AI