import os
import matplotlib.pyplot as plt
import numpy as np
#### 현재 경로 이동
os.chdir('/Users/frhyme/frhyme.github.io/_posts/personal_ipynb/')
#### 현재 파일 경로 변환
print(os.getcwd())
print("-"*20)
os.chdir('../../')
print(os.getcwd())
#### 현재 위치에 있는 폴더와 파일 프린트
print(list(filter( lambda x: True if "." not in x else False,
os.listdir())))
print("-"*20)
os.chdir('/Users/frhyme/frhyme.github.io/_posts/personal_ipynb/')
#### sample img generation
img = np.random.normal(0, 1, (200, 200))
file_name = '180703_os_test_img.svg'
plt.figure(), plt.imshow(img, cmap=plt.cm.gray), plt.axis('off')
plt.savefig(file_name), plt.close()
#### 파일 지우기
#### 둘다 똑같이 동작함.
print( file_name in os.listdir())
print(os.path.exists(file_name))
os.remove(file_name)
print( file_name in os.listdir())
print(os.path.exists(file_name))
print("-"*20)
/Users/frhyme/frhyme.github.io/_posts/personal_ipynb
--------------------
/Users/frhyme/frhyme.github.io
['_includes', '_posts', '_layouts', '_pages', 'Rakefile', '_site', 'Gemfile', '_data', '_sass', 'assets']
--------------------
True
True
False
False
--------------------
import os
#### bash command를 실행만 시키고 싶을 때
#### 하지만, 이 경우, 결과값을 반환하지 않음. 실행결과유무만 리턴됨
r = os.system('pwd')
print('success' if r==0 else 'failed')
print("-"*30)
import subprocess
#### 실행결과를 리턴받고 싶을때
#### 하지만, 이 때는 binary string으로 돌아오기 때문에, 변환해주는 것이 필요함.
for cmd in ['pwd', 'git status']:
binary_output = subprocess.check_output(cmd, shell=True)
print("## command: {}".format(cmd))
print(binary_output.decode('utf-8').strip())
print("-"*30)
success
------------------------------
## command: pwd
/Users/frhyme/frhyme.github.io/_posts/personal_ipynb
------------------------------
## command: git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: ../a_python_os.md
modified: 180703-os.ipynb
Untracked files:
(use "git add <file>..." to include in what will be committed)
../2018-07-03-binary_str_to_str.md
../2018-07-03-python_os.md
no changes added to commit (use "git add" and/or "git commit -a")
------------------------------
댓글남기기