2014-02-13 2 views
-1

"ls"로 현재 디렉토리의 내용을 나열한 다음, ls의 결과를 목록에 넣어서 할 수있는 스크립트를 작성하는 방법을 알아 내려고합니다 ls에있는 각 요소가있는 무언가. 나는 실제로 터미널에서이 작업을 수행하려고 시도하고 있습니다. 그런 다음 Python을 사용하여 스크립트를 작성하지만 터미널에서 작동하지도 않습니다.목록의 디렉토리 내용

아, 나는 파일로 LS의 출력을 리디렉션 시도했다하지만 난 그렇게 할 수있는 권한이하지 않는 것 :

>>> subprocess.call(["ls ", directory_expanded, " > ", "namesoutput.txt"]) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib64/python2.6/subprocess.py", line 478, in call 
    p = Popen(*popenargs, **kwargs) 
    File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__ 
    errread, errwrite) 
    File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child 
    raise child_exception 
OSError: [Errno 13] Permission denied 

답변

3

방법에 대한 os.listdir를?

In [25]: os.listdir('.') 
Out[25]: ['blah'] 
+0

이것은 가장 유망한 접근 방법입니다. +1 –

관련 문제