3/10/2014

Python study, OS modules

__author__ = 'mare'

import os

print( os.getcwd() )
#/Users/mare/PycharmProjects/OS_module

print( os.access('.', os.F_OK ) )
print( os.access('.', os.W_OK | os.X_OK | os.R_OK ) ) #write, exec, read
#True
#True

os.rmdir('test1')
print( os.listdir('.') )
#['.idea', 'osModule.py']
os.mkdir('test1')
print( os.listdir('.' ) )
#['.idea', 'osModule.py', 'test1']

os.removedirs('test2/sub1/sub2/leaf')
os.makedirs('test2/sub1/sub2/leaf')
print( os.listdir('test2/sub1/sub2') )
#['leaf']

print(os.listdir('.'))
os.rename('newfile.txt', 'oldfile.txt')
print(os.listdir('.'))
#['.idea', 'newfile.txt', 'osModule.py', 'test1', 'test2']
#['.idea', 'oldfile.txt', 'osModule.py', 'test1', 'test2']
os.rename('oldfile.txt','newfile.txt')


os.renames('newfile.txt', 'ttt/moved.txt')
print( os.listdir('ttt') )
#['.DS_Store', 'moved.txt']
os.renames('ttt/moved.txt', 'newfile.txt')

print( os.stat('newfile.txt') )
#posix.stat_result(st_mode=33188, st_ino=5325009, st_dev=16777219, st_nlink=1, st_uid=501, st_gid=20, st_size=0, st_atime=1394464185, st_mtime=1394463961, st_ctime=1394463961)


#I made directory like that
# - test_walk
#  +- a
#  +- b
#      +- newfile.txt

for path, dirs, files in os.walk('test_walk'):
 print( path, dirs, files)
#test_walk ['a', 'b'] ['.DS_Store']
#test_walk/a [] []
#test_walk/b [] ['newfile.txt']

for path, dirs, files in os.walk('test_walk', topdown=False):
 print( path, dirs, files)
#test_walk/a [] []
#test_walk/b [] ['newfile.txt']
#test_walk ['a', 'b'] ['.DS_Store']


print( os.pipe() ) #create pipe to communicate between proccess


#file object creation
#r, w = os.pipe()
#rd = os.fdopen(r)

#rp = os.popen('dir' 'r')
#print( p.read() )


print(os.name)
#posix

print( os.environ )
#environ({'__CF_USER_TEXT_ENCODING': '0x1F5:3:51', 'SSH_AUTH_SOCK': '/tmp/launch-XLOLCi/Listeners', 'PYCHARM_HOSTED': '1', 'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'PYTHONIOENCODING': 'UTF-8', 'PYTHONUNBUFFERED': '1', 'PATH': '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin', 'LOGNAME': 'mare', 'USER': 'mare', 'HOME': '/Users/mare', 'TMPDIR': '/var/folders/b7/gxpfjkqs0bs33cyyn76q7drw0000gn/T/', 'VERSIONER_PYTHON_VERSION': '2.7', '__CHECKFIX1436934': '1', 'Apple_PubSub_Socket_Render': '/tmp/launch-bHTzXo/Render', 'SHELL': '/bin/bash', '__PYVENV_LAUNCHER__': '/Library/Frameworks/Python.framework/Versions/3.3/bin/python3', 'PYTHONPATH': '/Users/mare/PycharmProjects/OS_module'})
print( os.environ.keys() )
#KeysView(environ({'__CF_USER_TEXT_ENCODING': '0x1F5:3:51', 'SSH_AUTH_SOCK': '/tmp/launch-XLOLCi/Listeners', 'PYTHONPATH': '/Users/mare/PycharmProjects/OS_module', 'LOGNAME': 'mare', 'PATH': '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin', 'PYCHARM_HOSTED': '1', '__CHECKFIX1436934': '1', 'PYTHONUNBUFFERED': '1', 'SHELL': '/bin/bash', 'Apple_PubSub_Socket_Render': '/tmp/launch-bHTzXo/Render', 'PYTHONIOENCODING': 'UTF-8', 'TMPDIR': '/var/folders/b7/gxpfjkqs0bs33cyyn76q7drw0000gn/T/', 'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'USER': 'mare', 'VERSIONER_PYTHON_VERSION': '2.7', '__PYVENV_LAUNCHER__': '/Library/Frameworks/Python.framework/Versions/3.3/bin/python3', 'HOME': '/Users/mare'}))

print( os.getpid() )
#10140

os.system('calc')
#os.startfile('LICENSE.txt')