Using some things in the module os


#1

First of all:
I am assuming that your are a python intermediate I will be talking about some things in the os module
maybe you have seen "C://" "E://" this is a beginning of path
what is a path?
a path is like a way to a place in the system like a file
well how can i know the path
you can do it by clicking the file with a right click and clicking properties you will see the location
this is the path the location of the file in the system
this is some paths of some files:
C:\Users\yossef\Desktop
E:\mstn.htm
you can specify the path like this
(the name of the local disk)(the name of the file)(followed by the file in the first file)
last thing :
what is a directory?
a directory is the place where the path refers to like a file but not an app
C:\Users\yossef\Desktop this is a directory
C:\Users\yossef\Desktop\t t is a file in the directory
okay let's get into some code
:
from os import listdir,rename,mkdir,chdir,getcwd,rmdir,system,walk # i will be talking about this 6 functions in the os module
listdir("C://") # if you typed this on your machine you will get this result:
['$Recycle.Bin', 'autoexec.bat', 'config.sys', 'Documents and Settings', 'hiberfil.sys', 'MinGW', 'MSOCache', 'pagefile.sys', 'PerfLogs', 'Program Files', 'ProgramData', 'Python27', 'Recovery', 'System Volume Information', 'TURBOC3', 'Users', 'Windows']
this is the files inside of C local disk
listdir("C:/Python27") the result will be :
['DLLs', 'Doc', 'error class.py', 'f.py', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'Scripts', 'tcl', 'Tools', 'w9xpopen.exe']
this is ten files iniside of Python27
okay let's talk about rename

rename('C:/Users/yossef/Desktop/yossef.txt','C:/Users/yossef/Desktop/yossef1.txt')
this renames a file named yossef to yossef1
okay very simple right lets get to to another function
well you might be thinking how to make a file with python this how:
mkdir('C:/users/yossef/Desktop/hey')
this how you make a file in python and put it on the desktop
chdir('C:/users/yossef/Desktop/hey')
this how i make the directory the current working directory
if i want to know what is the working directory i will type
getcwd() the result will be :
'C:/users/yossef/Desktop/hey'
if i want to make a file without specifying the file like that
mkdir('kalo')
python woll make this file inside of the hey file as it is the current working directory
if i want to remove hey file
rmdir('C:/users/yossef/Desktop/hey')
a error will pop up:
Traceback (most recent call last):
File "", line 1, in
os.rmdir('C:/users/yossef/Desktop/hey')
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/users/yossef/Desktop/hey'
because the directory is being used by python an error will pop up
well i wan to delete this dir i have to change the directory that python is working on
chdir("E:/newfile")
now i can remove the file
rmdir(''C:/users/yossef/Desktop/hey")
now we will use the system function
system("shutdown /p") # WARNING THIS WILL SHUTDOWN YOUR COMPUTER LOL
system is used to send system cmds
the last thing is the walk function
walk("C://")
this walks through all teh files in the c local disk and give the paths in it
this is like an explaining writing this is the first time i explain something i hope this helps forgive me for any misspelling words my english is good but because of my speed in writing
and i hope this help