Python의 os.listdir() 메서드는 지정된 디렉터리에 있는 모든 파일과 디렉터리의 목록을 가져오는 데 사용됩니다. 디렉터리를 지정하지 않으면 현재 작업 디렉터리의 파일 및 디렉터리 목록이 반환됩니다.
os.listdir() Python의 메소드 구문
통사론: os.listdir(경로)
매개변수 : path (선택 사항) : 디렉터리 경로
반환 유형: 이 메서드는 지정된 경로에 있는 모든 파일 및 디렉터리 목록을 반환합니다. 이 메소드의 반환 유형은 다음과 같습니다. 목록 .
Python os.listdir() 메서드 예
다음은 몇 가지 예입니다. 파이썬 os.listdir() 메소드의 OS 모듈 :
Python에서 파일 및 디렉터리 나열 os.listdir() 메소드 사용
이 예제에서 코드는 os.listdir()을 사용하여 루트 디렉터리(/)에 있는 파일 및 디렉터리 목록을 가져옵니다. 그런 다음 얻은 목록을 인쇄합니다. 출력에는 지정된 루트 디렉터리에 있는 파일과 디렉터리가 포함됩니다.
파이썬3
파이썬 프로그램
# importing os module> import> os> > # Get the list of all files and directories> path>=> '/'> dir_list>=> os.listdir(path)> > print>(>'Files and directories in ''>, path,>'' :'>)> > # print the list> print>(dir_list)> |
>
>
산출:
Files and directories in ' / ' : ['sys', 'run', 'tmp', 'boot', 'mnt', 'dev', 'proc', 'var', 'bin', 'lib64', 'usr', 'lib', 'srv', 'home', 'etc', 'opt', 'sbin', 'media']>
현재 디렉터리의 파일 및 디렉터리 나열 os.listdir() 사용
이 예제에서 코드는 os.listdir() 메서드를 활용하여 현재 작업 디렉터리에 있는 파일 및 디렉터리 목록을 가져옵니다. os.getcwd() 방법. 그런 다음 얻은 목록을 인쇄하여 현재 작업 디렉터리에 있는 파일 및 디렉터리에 대한 정보를 제공합니다.
파이썬3
# importing os module> import> os> > # Get the path of current working directory> path>=> os.getcwd()> > # Get the list of all files and directories> dir_list>=> os.listdir(path)> > print>(>'Files and directories in ''>, path,>'' :'>)> # print the list> print>(dir_list)> |
>
>
크루스칼 알고리즘'
산출:
Files and directories in ' /home/ihritik ' : ['.rstudio-desktop', '.gnome', '.ipython', '.cache', '.config', '.ssh', 'Public', 'Desktop', '.pki', 'R', '.bash_history', '.Rhistory', '.oracle_jre_usage', 'Music', '.ICEauthority', 'Documents', 'examples.desktop', '.swipl-dir-history', '.local', '.gnupg', '.profile', 'Pictures', '.keras', '.viminfo', '.thunderbird', 'Templates', '.bashrc', '.bash_logout', '.sudo_as_admin_successful', 'Videos', 'images', 'tf_wx_model', 'Downloads', '.mozilla', 'geeksforgeeks']>
경로가 지정되지 않은 경우 모든 파일 및 디렉터리 나열
이 예제에서 코드는 os.listdir()을 사용하여 현재 작업 디렉터리에 있는 파일 및 디렉터리 목록을 가져옵니다. 그런 다음 얻은 목록을 인쇄하여 현재 작업 디렉터리에 있는 파일 및 디렉터리에 대한 정보를 제공합니다. 경로를 지정하지 않으면 기본값은 현재 작업 디렉터리입니다.
파이썬3
라텍스의 부분 파생물
# importing os module> import> os> # os.listdir() method return path> dir_list>=> os.listdir()> print>(>'Files and directories in current working directory :'>)> # print the list> print>(dir_list)> |
>
>
산출:
Files and directories in current working directory : ['.rstudio-desktop', '.gnome', '.ipython', '.cache', '.config', '.ssh', 'Public', 'Desktop', '.pki', 'R', '.bash_history', '.Rhistory', '.oracle_jre_usage', 'Music', '.ICEauthority', 'Documents', 'examples.desktop', '.swipl-dir-history', '.local', '.gnupg', '.profile', 'Pictures', '.keras', '.viminfo', '.thunderbird', 'Templates', '.bashrc', '.bash_logout', '.sudo_as_admin_successful', 'Videos', 'images', 'tf_wx_model', 'Downloads', '.mozilla', 'geeksforgeeks']>
자주 묻는 질문(FAQ)
os.listdir() 메소드로 무엇을 이해합니까?
`os.listdir()`은 지정된 경로 또는 현재 작업 디렉토리에 있는 파일 및 디렉토리 목록을 얻는 데 사용되는 Python 메소드입니다. 경로가 제공되지 않으면 메서드는 목록을 반환하므로 Python 스크립트에서 파일 시스템 콘텐츠를 쉽게 탐색하고 조작할 수 있습니다.