Python for Ethical Hackers
Mohammad reza Kamalifard
Python Language Essentials
Module 2: System Programming
Part 2 :
Directory Navigation
Directory Details
Methods for traversing directories
Listing file and their information
Creating and deleting directories and files
Test to check if something is a file or directory
>>> import os
>>>
>>> os.getcwd()
'/home/reza/code/PYSEC101/dir'
>>>
>>>
>>>os.mkdir('class')
$ ls
Class jalase7 jalase7.py
os.rmdir()
>>> os.listdir('.')
['class', 'jalase7', 'jalase7.py']
>>>
>>> os.listdir('/')
['opt', 'initrd.img.old', 'lib', 'selinux', 'dev', 'home',
'initrd.img', 'lost+found', 'bin', 'usr', 'sys', 'lib64',
'windows', 'vmlinuz.old', 'proc', 'tmp', 'cdrom', 'run',
'sbin', 'mnt', 'root', 'boot', 'media', 'var', 'vmlinuz',
'etc', 'srv']
>>>
>>> for item in os.listdir('.'):
... if os.path.isfile(item):
... print item + ' is a file'
... elif os.path.isdir(item):
... print item + ' is a directory'
... else:
... print 'unknown'
...
Class is a directory
Jalase7 is a directory
jalase7.py is a file
>>>
This work is licensed under the Creative Commons
Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad reza Kamalifard.
All rights reserved.

اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی

  • 1.
    Python for EthicalHackers Mohammad reza Kamalifard
  • 2.
    Python Language Essentials Module2: System Programming Part 2 : Directory Navigation
  • 3.
    Directory Details Methods fortraversing directories Listing file and their information Creating and deleting directories and files Test to check if something is a file or directory
  • 4.
    >>> import os >>> >>>os.getcwd() '/home/reza/code/PYSEC101/dir' >>> >>> >>>os.mkdir('class') $ ls Class jalase7 jalase7.py os.rmdir()
  • 5.
    >>> os.listdir('.') ['class', 'jalase7','jalase7.py'] >>> >>> os.listdir('/') ['opt', 'initrd.img.old', 'lib', 'selinux', 'dev', 'home', 'initrd.img', 'lost+found', 'bin', 'usr', 'sys', 'lib64', 'windows', 'vmlinuz.old', 'proc', 'tmp', 'cdrom', 'run', 'sbin', 'mnt', 'root', 'boot', 'media', 'var', 'vmlinuz', 'etc', 'srv'] >>>
  • 6.
    >>> for itemin os.listdir('.'): ... if os.path.isfile(item): ... print item + ' is a file' ... elif os.path.isdir(item): ... print item + ' is a directory' ... else: ... print 'unknown' ... Class is a directory Jalase7 is a directory jalase7.py is a file >>>
  • 7.
    This work islicensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad reza Kamalifard. All rights reserved.