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, shutilCode
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.
