Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Python Tutorial - Modules #1

Python Tutorial - Modules #11



OS Docs

Note: I will be using Python 2.7

To start off I would like to explain that I am not fluent in Python but recently saw ѕнα∂σω thread which stated he was going to learn os so I thought to myself that I would to. So what I will be doing is hopefully doing a tutorial every 2-4 days on the OS module, which will both teach me and you.

In this tutorial we will be looking at the basics of the OS module.

First off all we will import the module, to do this we just simply
Code
import os

I will now show you how you are able to find out whether a certain directory exists.
Code
import os

path = "c:/users/william/pictures/pic.jpg"

if os.path.exists(path):
    print "Dir Exists!"
else:
    print "Dir Can Not Be Found!"
The code is pretty self explanatory, if the variable path exists it will return "Dir Exists!" if it doesn't it will return "Dir Can Not Be Found!"

Just that tiny bit of code could be used in so many ways for example, if that dir exists then you could move it using the shutil module or maybe you would delete that file.

If you got stuck on this tutorial then feel free to contact me.

I will happily listen to any ideas you may have also.

-Cyber Punk

(May 17, 2014 11:27 AM)Trisomy 21 Wrote:  Don't make a new thread for your next tut, just add on to this, this really isn't an OS module tutorial, it's just an explanation of how to check if a directory exists, and a very similar code can be found in 100 places by googling.

But the thing is that I cover a new thing each tutorial.
For example this one was checking if the dir exists but the next one shows you how to edit the dir with the shutil module.

Cyber Punk
(May 17, 2014 11:42 AM)Trisomy 21 Wrote:  Well then you should change your thread title to "How to check if directory exists", so people don't think your actually giving a tutorial on the OS module in its entirety.

good point, I will do this when I get home. 🤝
I have to agree with zeroday this is not really a tut on os module you did not even bother to add os.system()
Maybe I have mislead you because my goal of this whole thing is making a program using the OS module which opens doors to other modules and will help you understand the OS module.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[The Brotherhood] by HackersMar 16, 2026 08:20 AM 18 709 Mar 16, 2026 08:20 AM
Last Post: Stanley
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by Bane PopaMar 13, 2020 09:49 PM 5 229 Mar 13, 2020 09:49 PM
Last Post: Nulla Misericordia