Using some things in the module os


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:
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', '', '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

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:
this how you make a file in python and put it on the desktop
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 :
if i want to make a file without specifying the file like that
python woll make this file inside of the hey file as it is the current working directory
if i want to remove hey file
a error will pop up:
Traceback (most recent call last):
File "", line 1, in
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
now i can remove the file
now we will use the system function
system is used to send system cmds
the last thing is the walk function
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