이것은 코드입니다 :IO 작업 실패
def edit(aFile):
s = ''
filename = getMediaPath() + aFile
inputfile = open(filename, 'r')
read = inputfile.readlines()
inputfile.close()
for lines in read:
lines = lines.lower()
lines = lines.replace("it's", "this is")
lines = lines.capitalize()
s = s + str(lines)
newfile = getMediaPath() + 'happyEdited.txt'
x = open(newfile, 'w')
x.write(s)
x.close()
내가 오류가 "inputfile의 =" 라인에 있습니다. 그것은 말합니다 : "I/O operation failed. 파일을 읽으려고했는데 그럴 수 없습니다. 파일이 있는지 확인하십시오. 존재한다면 올바른 디렉토리/폴더를 지정 했습니까?"
a 파일을 미디어 경로가있는 문자열로 입력 해 보았습니다. 나는 파일을과 동일하게 설정하려고 시도했지만 미디어 경로와 동일하지만 아무 것도 작동하지 않습니다. 매개 변수를 꺼내서 aFile 코드를 .txt 파일 이름으로 바꾸면 코드가 작동합니다.
고마워요!
문자열과 같은 파일 이름을 처리하지 마십시오. [os.path.join] (http://docs.python.org/2/library/os.path.html#os.path.join)을 사용하여 부품 경로를 생성하십시오. – kwatford
실패한 줄 앞에 파일 이름을 인쇄해볼 수 있습니까? – vik
getMediaPath는 정확하게 슬래시를 인코딩합니까? – theodox