Hack Forums
[Python] Python Tutorial - OS & Shutil Module #3 - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Python Tutorial - OS & Shutil Module #3 (/showthread.php?tid=4235562)

Pages: Pages 1 2


Python Tutorial - OS & Shutil Module #3 - #BORNE - May 21, 2014

Python Tutorial - OS Module #3



OS Docs
Shutil Docs

If you have not read Python Tutorial - Modules #1 and Python Tutorial - Modules #2 I recommend you go and read them before reading this one.

In this tutorial we will be using another module called Shutil which will allows us to have a variety of options to edit or move files around.
Like I said in the last tutorial we are going to be adding more options to our little program.

First we are going to import Shutil, to do this all we have to do is follow OS by a comma for example
Code
import OS, shutil
Now we have the module imported we are able to use its assets.

Code
import os, shutil

path = "c:/users/william/pictures/pic.jpg"
path2 = "c:/users/william/pictures/hello.jpg"
moveDir = "c:/users/william/desktop"

if os.path.exists(path):
    print "Dir Exists!"
    print "Options:\n1 Remove Directory\n2 Move Directory\n3 Rename"
    q = raw_input("What would you like to do? ")
    if q == ("1"):
        os.remove(path)
    elif q == ("2"):
        shutil.move(path, moveDir)
    elif q == ("3"):
        os.renames(path, path2)
    else:
        print "Option does not exist!"

else:
    print "Dir Can Not Be Found!"

As you can see we have the same code from the last tutorial but we have used to shuil module to create an option which allows you to more that file to a different directory.
Idea: You could ask for the users input for a directory of there choice, but this will be fine for the tutorial.

We have also added a function called os.renames which will allow you to change the name of that file.
Note: Look at the top of the code and notice I have added a new variable called Path2, what it will do is locate the variable Path and then change it to Path2.



* - ZXshockaXZ - May 21, 2014

I believe you want "Python Tutorial - OS Module #2" to say number 3?

Looks great man. I'll bet you're helping a lot of people out. Keep up the HQ posts.


* - #BORNE - May 21, 2014

(May 21, 2014 11:17 AM)ZXshockaXZ Wrote:  I believe you want "Python Tutorial - OS Module #2" to say number 3?

Looks great man. I'll bet you're helping a lot of people out. Keep up the HQ posts.

Edited the thread, thanks alot for the support

-Cyber Punk


* - dberube - May 21, 2014

Oh big bang. Are you seriously going to create a thread for every python functions in the standard library?
Because that would mean LOTS of threads. You could even start a cloth factory!


* - #BORNE - May 21, 2014

(May 21, 2014 12:14 PM)dberube Wrote:  Oh big bang. Are you seriously going to create a thread for every python functions in the standard library?
Because that would mean LOTS of threads. You could even start a cloth factory!

Hahahah, I love reading posts for children. :)


* - Firmus - May 21, 2014

It's pretty useful, but why don't put all of these into a big thread?


* - ZXshockaXZ - May 22, 2014

I agree that a thread for every function in the standard lib could be a bit much. I suppose it just depends on how far you plan on going with this.
If you plan on making this a pretty big chain, you could try dividing it up into "chapters" that contain multiple modules. Such as modules 1-10 on one thread, 11-20 on another.

Either way, you're contributing to the community, and you're helping people out. I still applaud you.


* - deiski - May 23, 2014

Please stop recreating a thread each time.. just put it in one thread, I don't like it clashing in and putting important threads down onto higher pages.


* - ѕнα∂σω - May 23, 2014

As everyone has said, it's probably a good idea to put all of these into one thread. This way it's a bit spam-like. I understand that you're trying to help, but I recommend you put it into one tutorial thread.

Otherwise, good job.


* - deiski - May 23, 2014

(May 23, 2014 08:34 PM)ѕнα∂σω Wrote:  As everyone has said, it's probably a good idea to put all of these into one thread. This way it's a bit spam-like. I understand that you're trying to help, but I recommend you put it into one tutorial thread.

Otherwise, good job.

We tell him this every time and he doesn't listen.


* - #BORNE - May 24, 2014

(May 23, 2014 09:33 PM)+Cyan Wrote:  We tell him this every time and he doesn't listen.

HAHAHAHAHHAHHA Sorry Cyan master of hf, ahah dont tell me what to do.


* - ѕнα∂σω - May 24, 2014

(May 23, 2014 09:33 PM)+Cyan Wrote:  We tell him this every time and he doesn't listen.

Well, what can you do. If he wants to spam the section, let him. I still don't get why he's so set on making a thread for every one of them, but oh well.


* - deiski - May 24, 2014

(May 24, 2014 08:08 AM)ѕнα∂σω Wrote:  Well, what can you do. If he wants to spam the section, let him. I still don't get why he's so set on making a thread for every one of them, but oh well.

I will never understand people like this, yeah sure do it for every module, but not for the same module.


* - dberube - May 24, 2014

Said the people who [b word redacted] his thread. 🤣


* - deiski - May 24, 2014

(May 24, 2014 10:36 AM)dberube Wrote:  Said the people who [b word redacted] his thread. 🤣

What the fuck is b word redacted? 🤦‍♂