2013-07-22 2 views
1
나는이 라인에 문제가있어
import os 
from os import stat 
from pwd import getpwuid 

searchFolder = raw_input("Type in the directory you wish to search e.g \n /Users/bubble/Desktop/ \n\n\n") 
resultsTxtLocation = raw_input("FIBS saves the results in a txt file. Where would you like to save results.txt? \nMust look like this: /users/bubble/desktop/workfile.txt \n\n\n") 

with open(resultsTxtLocation,'w') as f: 
    f.write('You searched the following directory: \n' + searchFolder + '\n\n\n') 
    f.write('Results for custom search: \n\n\n') 
     for root, dirs, files in os.walk(searchFolder): 
      for file in files: 
        pathName = os.path.join(root,file) 
        print pathName 
        print os.path.getsize(pathName) 
        print 
        print stat(searchFolder).st_uid 
        print getpwuid(stat(searchFolder).st_uid).pw_name 
        f.write('UID: \n'.format(stat(searchFolder).st_uid)) 
        f.write('{}\n'.format(pathName)) 
        f.write('Size in Bytes: {}\n\n'.format(os.path.getsize(pathName))) 

:파이썬 - f.write 1 개 인자, 2 주어진

f.write('UID: \n'.format(stat(searchFolder).st_uid)) 

내가 { '무엇을 모르는} \ n'.format는 않지만, 사람 이전 질문에서 제안 했으므로 여기서는 효과가있을 것이라고 생각했지만 그렇지 않습니다.

UID : 바이트에서 /사용자/거품/데스크탑/플롯 2.docx 크기 : 출력 텍스트 파일 내부

, 방금 다음 수 110,549

을하지만 말을해야 : UID를 : 501

f.write가 두 개의 인수를 이해하고 txt 파일에 기록하도록하려면 어떻게해야합니까?

많은 감사

+1

'.write()'에 전달한 문자열에'{}'을 잊어 버렸습니다. –

+0

고마워, 나는 그것을 덧붙였다. {} 정확히 뭐하는거야 ?? 어떻게 그 간단한 대괄호와 함께 작동합니까 ?? – BubbleMonster

+0

'.format()'의 매개 변수에 대한 자리 표시 자입니다 ([형식 문자열 구문] (http://docs.python.org/2/library/string.html#formatstrings) 참조). –

답변

2

'UID: {}\n'.format(stat(searchFolder).st_uid). {}이 없으면 {}: \n을 반환합니다.

이것은 문자열 형식입니다. {}은 대체 필드를 나타냅니다. doc을 읽을 수 있습니다.

+0

시간 내 주셔서 감사합니다. 장항유 – BubbleMonster

2

f.write('UID: {0}\n'.format(stat(searchFolder).st_uid)) 
f.write('{0}\n'.format(pathName)) 
f.write('Size in Bytes: {0}\n\n'.format(os.path.getsize(pathName))) 

Look at this answer에 변화

f.write('UID: \n'.format(stat(searchFolder).st_uid)) 
f.write('{}\n'.format(pathName)) 
f.write('Size in Bytes: {}\n\n'.format(os.path.getsize(pathName))) 

python documentation에 문자열 포맷에 대해 배울 수 있습니다.

+0

감사합니다. 매우 감사. – BubbleMonster

관련 문제