Python 3 Programming Tutorial – OS Module




In this video, we cover the OS module with Python 3. The main purpose of the OS module is to interact with your operating system. The primary use I find for it is to create folders, remove folders, move folders, and sometimes change the working directory.

Sample code for this basics series:

Python 3 Programming tutorial Playlist:

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

source

47 thoughts on “Python 3 Programming Tutorial – OS Module”

  1. I have read that subprocess module would replace os. If so, I would be glad if you make a tutorial of subprocess module as well.

  2. hey quick question.  so I tried os.mkdir('newDir1') in notepad++ and nothing happened when run( I lied its in the programs/notepad++ folder), but it worked in IDLE.  how would you do some of these commands from a static np++ doc but indicating a path different from the above path?

      Im familiar with windows powershell basic commands to make folders, move, copy, change dir, mkdir, rmdir, so I like using powershell for that, but its still good to know the same commands through python.  how would I do some of these through a static doc instead of IDLE which I try to avoid.   BTW Im chugging through your tuts and want to say youre an excellent teacher.  Gonna hit you up for more advice once Im done with the basics.  Thanks bro

  3. Just subscribed. Loved the video, I coded it all and thats really cool. I liked your help(os) feature you reminded us of, thats a good one not to forget. I do have a quick question, why is it that for my Python (2.7) it did work with yours, but I am curious why os.pardir() isn't working. I just tryed it a couple ways and none of them worked:

    curDir = os.mkdir('myDir')
    curDir.pardir() # didnt work

    os.pardir('anyDir') # didnt work

    os.pardir() # didn't work either

    Im not sure what's wrong or why I can't use that feature to tell my the parent directory

  4. ok, I'm already happy. I undesrtood almost everything. For an absolute beginner thats the achievement of the day 🙂 I just couldn't figure it out yet what time.sleep makes ..

  5. I'm working on a project for school (a programme that can open a file and manipulate the contents). Is it possible to use the os module to allow the user to open a file from another directory. When using the built in open function, I always get a Unicode escape error.

  6. I really like your tutorials so far.  I was wondering if you could clear something up for me.

    Let's say I start off a little script with:

    import os
    main_folderpath = os.path.abspath("C:\UsersPublicMain Folder")

    And then let's say I have some code in here that takes user input for a class title, a section title, and a section-part title, and after doing a little uniform text processing, I make three variables for the input; class_folder, section_folder, and section_part_folder.

    I tried to do a little magic with this form but I'm missing something:

    folderpath = os.path.join(main_folderpath + "\" + class_folder + "\" + section_folder + "\" + section_part_folder)
    os.mkdir(folderpath)

    I expected this set of folders to be made: C:\UsersPublicMain FolderClass-TitleSection-TitleSection-Part

    I got an error, though…Python telling me there was no such location, although an earlier mistake of leaving out the escaped backslashes successfully made an errant folder at: C:\UsersPublicMain FolderClass-TitleSection-TitleSection-Part

    – – –

    Does Python not create the folders in order…meaning, first mkdir makes a folder called 'Class-Title,' and then makes the next folder…or is the problem that you have to make the folders, one at a time, each a separate command?  Hopefully, that's not as jumbled as it is in my head.

  7. I am looking in the help file for the module and i found a command called abort()… It says that it Aborts the interpreter immediately and 'Dumps core' or otherwise fails in the hardest way possible on the hosting OS. Im scared given that i have a feeling this means it takes your CPU to the blue screen of death. Please tell me im worng.

  8. It would be nice if you make a tutorial how to make batch renamer for particular files in a folder (.jpg) using only os module

  9. Hey sentdex!!!, Thanks for the short tutorial. I need to know which software you are using to Python scripts. And is there any way to import a file into some other applications like Maya. Please let me know

  10. Hi Harrison, what is the syntax in python for change current directory to another directory? the equivalent code in command line is cd .

  11. I'm really curious about a code. I hope you help me.

    os.system("sed -i -e '1d' " + txt)

    what is mean of this code. what is it for?

  12. Couldn't you just os.system("rd folder")
    instead of os.rmdir("folder")
    and just use the os.system("CMD COMMAND") instead of learning all of the different os.commands?

  13. harrison, for some reason when i run os.getcwd() when I have anaconda open it runs properly and tells me where the file is. but when i want to double click it and my editor is not open it throws a error saying that i don't have permissions. and it's because the os.getcwd() is not returning the correct path it is actually returning somethings like: C:Windowssystem32
    Do you know how i can fix this? thank you.

  14. when I do this:
    ____________________________________________________
    import time, os

    dtime = "10"
    os.mkdir("This Directory Will Self Destruct In " + dtime)

    for i in range(10 ,0, -1):
    time.sleep(1)
    dtime = str(int(dtime) – 1)
    os.rename("This Directory Will Self Destruct In "+str(int(dtime)+1),"This Directory Will Self Destruct In " + dtime)

    os.rmdir ("This Directory Will Self Destruct In " + dtime)
    —————————————————————————————-
    It skips some numbers, anyone knows why?

  15. While watching this I took it upon myself to make a infinite loop that endlessly created folders. I got to over 250,000 in like 10 seconds.. I have a lot of regret.

    Love the tutorials btw keep it up!

  16. A little joke from me 😀 My gf said it was creepy 😀

    import os
    import time
    import random
    oldnum = 0
    os.mkdir('0')

    for num in range(1,1000000000000000000000000000000001):
    number = random.randint(1, 1000000000000000000)
    os.rename(str(oldnum),str(number))
    oldnum = number
    time.sleep(1)

  17. I know this comment is super late but I am creating a program using this to make a template folder for a web project, so it will have a named folder of the users choice, they can name the HTML and CSS file and it will create them and store them in the correct folders along with folders for other necessitates such as images and Javascript code. My question is how do I change the target directory so that it doesn't create the template folder in the same directory as the program?

    Love this series btw so happy I found your channel

  18. I keep using the same file for every video and just retyping code. Should I just do a save as for every new video?

Leave a Reply