Python 2.7 provides the module os to interact with the OS you are operating the python within. First of all you need to open your IDLE and import the os module by typing - import os and then to find your current working directory type os.getcwd() ;
>>os.getcwd()
this will show the current working directory of the python programming language. You will get something like 'C:\\Python27'
If you want to change the current working directory you should write something like this -
>> os.chdir('new working directory')
To make a new directory, you have to use the mkdir() function with the directory name as argument.e.g.
>> os.mkdir('/newdirctory') # or you can also make multiple directory by using this -
>>os.makedirs('/newdir/newdir/newdir')
Similarly to remove a directory use the os.rmdir('/newdirectory'). and to remove a file use os.remove('filename')
You can the list of files and folder in a directory by using the listdir() utility function.
>> os.listdir(os.getcwd())
This will print the files and folder in your current working directory.
To see all the environment variables in your current os, type -
>>os.environ
It will print something like this -
{'TMP': 'C:\\Users\\ITDEPT~1\\AppData\\Local\\Temp', 'COMPUTERNAME': 'ITDEPT4', 'USERDOMAIN': 'ITDept4', 'PSMODULEPATH': 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules\\', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 61 Stepping 4, GenuineIntel', 'PROGRAMFILES': 'C:\\Program Files', 'PROCESSOR_REVISION': '3d04', 'SYSTEMROOT': 'C:\\WINDOWS', 'HOME': 'C:\\Users\\I.....etc
To rename a file use os.rename() command with the old name first and then the new name.
>>os.getcwd()
this will show the current working directory of the python programming language. You will get something like 'C:\\Python27'
If you want to change the current working directory you should write something like this -
>> os.chdir('new working directory')
To make a new directory, you have to use the mkdir() function with the directory name as argument.e.g.
>> os.mkdir('/newdirctory') # or you can also make multiple directory by using this -
>>os.makedirs('/newdir/newdir/newdir')
Similarly to remove a directory use the os.rmdir('/newdirectory'). and to remove a file use os.remove('filename')
You can the list of files and folder in a directory by using the listdir() utility function.
>> os.listdir(os.getcwd())
This will print the files and folder in your current working directory.
To see all the environment variables in your current os, type -
>>os.environ
It will print something like this -
{'TMP': 'C:\\Users\\ITDEPT~1\\AppData\\Local\\Temp', 'COMPUTERNAME': 'ITDEPT4', 'USERDOMAIN': 'ITDept4', 'PSMODULEPATH': 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules\\', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 61 Stepping 4, GenuineIntel', 'PROGRAMFILES': 'C:\\Program Files', 'PROCESSOR_REVISION': '3d04', 'SYSTEMROOT': 'C:\\WINDOWS', 'HOME': 'C:\\Users\\I.....etc
To rename a file use os.rename() command with the old name first and then the new name.
No comments:
Post a Comment