Python Tutorial: OS Module - Use Underlying Operating System Functionality
Вставка
- Опубліковано 5 кві 2016
- In this Python Tutorial, we will be going over the 'os' module. The os module allows us to access functionality of the underlying operating system. So we can perform tasks such as: navigate the file system, obtain file information, rename files, search directory trees, fetch environment variables, and many other operations. We will cover a lot of what the os module has to offer in this tutorial, so let's get started.
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python
+ os.getcwd() => get current working directory
+ os.chdir() => change directory
+ os.listdir() => list directory
+ os.mkdir() => create a directory
+ os.makedirs() => make directories recursively
+ os.rmdir() => remove directory
+ os.removedirs() => remove directory recursively
+ os.rename(, ) => rename file
+ os.stat() => print all info of a file
+ os.walk() => traverse directory recursively
+ os.environ => get environment variables
+ os.path.join(, ) => join path without worrying about /
+ os.path.basename() => get basename
+ os.path.dirname() => get dirname
+ os.path.exists() => check if the path exists or not
+ os.path.splitext() => split path and file extension
+ dir(os) => check what methods exists
thank you best friend
Thank you best friend forever.
thanks bro
Thank you best friend
thank you bro
its so sad that this guy has not uploaded any content for over a year now... his tutorials are always the best, most methodically thought out pieces of work on the platform
You already finished all 143 of the python videos?
Nobody cares that some 15 year old girl working for the Mexican cartels goes around raping dude to try and extort them.
Nobody gives a shit.
I agree .. he is sorely missed
yeah right 😞
He’s back in action now!
A tutorial for the sys module would help tremendously. It's difficult to find someone like you who can explain things in such detail as well as show how useful certain things within python can be without half-assing it. Thanks, Corey.
It is very excellent tutorial, much better than some paid python tutorial vedios in Udemy
for i in range(1,1000000):
print('Thank you so much for sharing this wonderful tutorial')
while True:
print('Thank you so much for sharing this wonderful tutorial')
(Python script is in c:/pyproj/thankyou.py)
Import subsystem
Subsystem.call([C://Python//Python.exe, C://pyproj//thankyou.py])
while True:
Print(“Thank You So Much”)
@@Pr0G4m3R4u 😂😂😂
@@greenballscience9514 it should be like:
import os
os.system(''C://Python//Python.exe, C://pyproj//thankyou.py")
Error, wait for script to finish..
.. CTRL+C
>>> python Greenball_Science.py
now you were saying?
The best one i have ever seen in teaching coding . Thank you so much man
Bless your soul Corey! The os module has always been such a pain to learn. When you first see it in code it's so cryptic. Thanks for breaking it down for us!
These Python videos are extremely helpful, thank you. I would be grateful if you added chapters. Some of the 10 minute plus videos have a lot of concepts and it would be great to jump back into a specific section when in need of a refresher. Keep up the excellent work and stay Pythonic my friend.
Pythonic?
You are very good at explaining Python modules and their practical uses.
For windows users, print (os.environ.get("HOME")) won't work as there is no HOME folder, instead it is USERPROFILE. So, type USERPROFILE instead of HOME.
THANKS A LOT!!! i just can't get it work until i saw this comment. but why i can't get it to search other folders? am i only able to search within the same tree?
Thank you. Now I made this who gives you the desktop path.Pretty useful if you want to create something in the desktop.
os.path.join(os.environ.get("USERPROFILE"), "Desktop")
@murakumo222 I don't know if you finally did it but assuming you are referring to os.walk() you could do the os.walk('C:\\Users') replacing the string with whatever directory you want the tree on.
Sukant Arora os.environ.get('HOME') works just fine for me
os.environ.get("HOME") worked fine for me in Windows 10
I was surprised how quick that search was wth os.walk().... and at the same time, wondered why Windows "Find" takes so long. Thanks for the great teachings.
It is so demonstrating that you can hardly find any questions in the comments - there are only thanks and insparations 😏 The explanation is so clear that it leaves nothing to ask about 💎 Great job 👍
every time I need a tutorial video for Python, Corey is always the best one with really well organized, fully information and easily to remember. Thanks a lot Corey
Your grammar is wrong, the quote should be, "Every time I need a tutorial video for Python, Corey is always the best one with really well organized, full information and easy to remember. Thanks a lot Corey."
@@codingisthebest3914 Ich würde mal sagen,dass du ein Klugscheißer bist :)
@@codingisthebest3914 Based on his name, he's not American, so it's fine. I agree with what oneLittleDuck wrote in German. LOL
Simple, well organized, great for beginners and informative. Nothing but least speaks English doesn't try to showoff.
Thanks Corey. I have started following you.. You make things so simple when it comes to using necessary commands in python.
for Windows users I used HOMEPATH instead of Home , use SET Command to be able to see all environment variables on CMD
print(os.environ.get('HOMEPATH'))
Thank you! I've been using the os module for years and had no idea about os.walk()! Will be putting this to good use!
You are an excellent presenter. The details were very well explained. I learned a lot. Thank you
Excellent! I really appreciate you sharing so much knowledge at no cost. I am getting it slowly. Thank you
you are much better than my faculty handling python course....
I've been learning python for several months now, and not once did i ever see anything about print(dir())! Like given!
I never read your descriptions, but after seeing what's on your amazons' wish list, I can tell, you aint no ordinary tutor. Respect. Sharp, evolving mind.
hey are you programmer now?
@@engr.mubasharhanif Hey. still and will continue to hold myself newbie, but I do have production web app running that I charge the client for.
Thank you Corey Schafer you're the best teacher for pyhton
Great tutorial straight to point teaching the stuff that actually creates complex programs in a simple way is great
Super good whistlestop tour. Just what I needed.
wonderful explanation.... Thanks a lot for making it more clear brother.
you should honestly be a teacher.
Very nice! Tight and down to the core of what is needed!
Thank you Corey for your time and effort. Keep spreading the knowledge.
You teach in a interesting way sir ,Thanks Today i have learned all things i was trying to learn from many days
Extremely useful and explained so well...thanks Corey!
Corey , thank YOU at the highest degree !
OMG, you are really good! Very clear voice and excellent explanation. Thanks for share.
I love this video tutorial just like many :-). As a systems admin, I would like to learn more about the os module. If possible you can make an advanced tutorial on this.
An excellent tutorial. Learnt new stuff and this video helped me clear some doubts. Thanks a lot!!!
please make one tutorial for sys module as well... :-)
yeah thumbs up!
Yeah Corey! one for the sys module please! :)
and subprocess ;-)
and pdb debugger as well if possible :)
Veeresh Aradhya yes please. Been struggling with stdin and stdout
Thanks a lot i started my cyber security journey yesterday , I was advised to learn this module thanks!!!!
thanks for this video, clear and helpful and I learnt some new stuff. Please keep on releasing more!
I love Corey's teaching ... and accent!
Another excellent tutorial! 💯
Thanks a lot Corey 🙏
Corey, I hope you are well today. 🤙🤙 I have been sober for one year. We got this.
Very helpful guide to such an import module :) thanks!
Great tutorials, extremely helpful and easy to understand! Which Python Interpreter are you using for coding in python? This looks very quick and easy to show putput.
First of all, your teaching style is good, and anyone grabs ur teachings easily, and thanks for teaching us. you are the best tutor I have ever come up with in python language. can u suggest any python language book, which is best to go through other than your channel? Moreover, I am a beginner to python language I thoroughly enjoyed ur videos, if u can suggest me best python book to ur sensibility and I guess it suits me and it helps me to learn further (I like physical like books). In case if u written any books related to this language can u suggest one (or) your favorite book, please......................................
Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.
@Corey Schafer. Your tutorials are awesome! Thank you! :)
Good, useful. Lots of stuff in the os module, that is for sure.
Very informative and helpful. And also very well explained. Thank you
Thanks, it was very useful for me as a python beginner.
Excellent explanations and examples, thank you!
What a very helpful tutorial, I owe you more than a subscription
Great tutorial. Simple and straight.
Really really good stuff Corey!
Thank you for the clear and concise explanation!
This was a very good video lesson for me as a beginner. I signed up for a Udemy class but found the content on OS Module functionality left me with some questions. I will definitely be using your videos as a resource1
Which Udemy python class are you taking? I plan on taking a Udemy class for Python to supplement what I learn from Corey's channel. Thanks.
When you write a ''print (dir (os))'' so show me the options at the bottom and do not know if I have a well-adjusted Sublime Text 3?
Hi Corey
How can I call bash function from Python , where function name passed as command line argument from Python script.
Thanks
Awesome 👍🏼.
Great explanation
This si pretty interesting, Corey!! Thanks a lot for your help!
Please post one lecture on data structure and algorithm. Thanks for your fantastic work as always
This a very good tutorial.Thank you Corey
Thanks bro!!! its very helpful... Good luck
Awesome one to start with. Just a lot of things.
how can i open an exe file and feed some input to that opened application and get the output out of it ?? is it able to do in os module ??
Thank you Corey. Truly appreciate what you share.
grate video am always around and always learning somthing new from the same video
Hey Corey, Do you have any tutorial for sys module...Thanks
Thank you for the video..... Learnt new stuff from this.....
super super super. excellent teaching skills.
Thats so brilliantly explained. Thanks a lot !!!!
You are the greatest Python teacher
simply awesome.. thanks Corey :)
For anyone who needs to work with paths and don't want to deal with the forward/backward slashes difference between Linux, Windows, and MacOS, you can consider or at least take a look at the module `Pathlib`. It is simpler and easier to manipulate in my opinion.
My favourite part of the module is its slash operator, e.g. taken from the documentation
```python
>>> p = PurePath('/etc')
>>> p
PurePosixPath('/etc')
>>> p / 'init.d' / 'apache2'
PurePosixPath('/etc/init.d/apache2')
```
Thus, you are free from the long `os.path.join()` method call introduced in 14:05 and make things cleaner and more readable.
Great video,
I was wondering, how can i find the last created file in a directory?
Thanks
hey corey . i want to know that how you can search on the ssearch command bar in windows and then open it without having its dir like any of the pc do if you provide filename and it shows possible results
Thank you very very much for these videos. Learning a lot from your videos.
Hi, Corey. How do you know about all of these module functions and methods and how they work? For example, in Linux you can look up man pages for commands but what do we have for these methods? Just googling the documentation?
Hello Schafer, your tutorial are really fruitful for python beginners like me. I just asking one question,
Is there any launcher file required to start python project. As we know in java there is a main method and in Android there is a launcher activity?
Thanks a lot.
I am a huge fan of yours .. like countless others. I have been telling about this python playlist to each and everyone of my colleagues who are interested in python. I also emphasize that wander nowhere else. This is the place to be.
On a side, How do you feel Corey to read such overflowing wonderful comments on you and your work? Would really like to know :)
I'm extremely grateful for all of the kind comments. I love reading comments from you all each morning.
very clear and precise. thank you.
thank youuu! your tutorials are awesome!
Great vid! Very useful
Amazing the simplicity of python :O
Thank you, Sir. Your videos are extremely helpful.
This is an important suggestion for tutorials. Use Jupyter notebooks for tutorials. You won't have to erase your codes and output is line by line which is really easy to understand.
I enjoy using Jupyter notebooks and have a video on how others can begin using them, but I have found that for tutorials it is best to stick with simple editors that beginners are more familiar with. They may become confused when their code with a notebook doesn't run top-to-bottom like a typical script. Thank you for the suggestion though. I will think more about it.
@@coreyms exactly right sir Corey Scafer, you always think about your community....that's the real quality of a classic teacher :)
..also in Jupyter nb you may have to restart kernel or delete that cells to wash off unwanted variables from your code snippet..so I too think Sublime is best option from a learners point of view
now I understand this OS & good use case for file manipulation!
THANK YOU COREY!
Thanks Corey bloody good show old chap!!!
Thank you so much. It was super helpful
Thank you. This was super cool.
Hello, how can I change the program with os.walk() to search through entire file until it finds what I'm looking for ? Thank you in advance for helping me out.
Hey, can someone please explain when and why I get double slashes in some "path" strings? For example when I print(os.environ): "ALLUSERSPROFILE': 'C:\\ProgramData'"?
Hi Corey,
Thank you for making this tutorial, that was very useful.
Could you please make a tut. on the SYS Module?
I think, there are so many people on your channel requested for that multiple times but requesting again.
Hi Correy, is there any command to know about what version of 'OS Module' we have installed, since there are no __version__ or version attribute available in OS Module ?
Is there a way to import a file into your code using the file explorer for windows (Something similar to CTRL+O)?
nice tutorial. i was however thinking you would also cover relative path traversing. Like if you want to go one step up the directory without concerning yourself with the full absolute directory path
Amazing and insightful video!
I have a command(net backup command that runs on Linux and Solaris) that produces output in multiple integers, now how do I create a list out of it and run it in a loop..??
very informative tutorial, thanks
How would I create a main directory, and then multiple sub directories (No other sub directories in the sub directories, all directly through the main directory) inside that main directory in the same line of code?
thank you Sir helped my work a lot
Awesome tutorial!
in 10:05 how you indented 3 lines at the same time? do you know the shortcut in vsc?