본문 바로가기
Python

[Python] IPython shell 에서 shell cmds 사용하기.

by ds31x 2023. 9. 19.

Python interactive shell에서 OS등이 지원하는 shell commands를 사용하지 못하는 것과 달리,

IPython shell에서는 많이 사용되는 shell commands는 다음과 같이 사용가능함.

In [17]: pwd
Out[17]: '/home/dsaint31'

In [18]: ls
lectures@    Mambaforge.sh*  Miniconda3-latest-Linux-x86_64.sh*  test/
mambaforge/  miniconda3/     pdfsizeopt/

In [19]: cd pdfsizeopt/
/home/dsaint31/pdfsizeopt

In [20]: ls
pdfsizeopt_libexec/  pdfsizeopt_libexec_linux.tar.gz  pdfsizeopt.single*

In [21]: mkdir test

In [22]: ls
pdfsizeopt_libexec/  pdfsizeopt_libexec_linux.tar.gz  pdfsizeopt.single*  test/

In [23]: mv pdfsizeopt_libexec_linux.tar.gz ./test/

In [24]: ls
pdfsizeopt_libexec/  pdfsizeopt.single*  test/

In [25]: cd test
/home/dsaint31/pdfsizeopt/test

In [26]: ls
pdfsizeopt_libexec_linux.tar.gz

In [27]:
  • In [숫자] : 사용자의 input을 대기하는 prompt라고 볼 수 있다. square bracket 안의 숫자는 일종의 실행 순서를 나타내는 번호이다.

위의 예제는 automagicON상태에서 동작된다. (magic commands가 % prefix 없이 수행가능한 상태)
만약 automagicOFF 상태라면 % prefix를 붙여야 함.



IPython이 제공하는 shell-like magic commands 는 다음과 같음.

%cat, %cp, %env, %ls, %man, %mkdir, %more, %mv, %pwd, %rm, %rmdir


위에서 살펴본 commands 외에 OS에서 지원하는 shell commands를 실행하려면 prefix로 !를 붙여주면 된다.

In [27]: !hostname -I
172.30.205.73

In [28]:
  • 해당 시스템의 ip 주소를 확인하는 명령어임.

Shell commands의 결과를 변수로 받아서 Python에서 사용할 수도 있고, Python의 변수를 curly bracket을 이용하여 shell commands에 넘겨줄 수 도 있다.

In [38]: c = !ls

In [39]: print(c)
['pdfsizeopt_libexec', 'pdfsizeopt.single', 'test']

In [40]: c = "Hello, IPython!"

In [41]: !echo {c}
Hello, IPython!

In [42]:

읽어보면 좋은 자료

https://dsaint31.me/mkdocs_site/OS/linux_cmds/#_13

 

BME228

Linux : 명령어 현재 디렉토리 확인 현재 디렉터리를 확인하는 명령어는 pwd(print working directory)이다. 현재 위치를 확인한다. 즉, 현재 디렉터리의 절대 경로를 출력한다. 디렉토리 이동 다른 사용자

dsaint31.me

https://swcarpentry.github.io/shell-novice/

 

The Unix Shell: Summary and Setup

Summary and Setup The Unix shell has been around longer than most of its users have been alive. It has survived because it’s a powerful tool that allows users to perform complex and powerful tasks, often with just a few keystrokes or lines of code. It he

swcarpentry.github.io

https://jakevdp.github.io/PythonDataScienceHandbook/01.05-ipython-and-shell-commands.html

 

IPython and Shell Commands | Python Data Science Handbook

Quick Introduction to the Shell¶ A full intro to using the shell/terminal/command-line is well beyond the scope of this chapter, but for the uninitiated we will offer a quick introduction here. The shell is a way to interact textually with your computer.

jakevdp.github.io

 

728x90