2014-08-29 1 views
0

큰 sh 파일을 작은 sh 파일로 나누는 python 스크립트를 만든 다음 모든 sh 파일을 새 화면으로 보냅니다. 이 코드TypeError : 유니 코드로 강제 변환 : 쉘 스크립트를 만들 때 찾은 문자열 또는 버퍼 파일.

import os 

splitLen = 2   # 2 lines per file 
outputBase = 'US11_' 
# This is shorthand and not friendly with memory 
# on very large files (Sean Cavanagh), but it works. 
input = open('US11.sh', 'r').read().split('\n') 
at = 1 
for lines in range(0, len(input), splitLen): 
    # First, get the list slice 
    outputData = input[lines:lines+splitLen] 

    # Now open the output file, join the new slice with newlines 
    # and write it out. Then close the file. 
    output = open(outputBase + str(at) + '.sh', 'w') 
    output.write('#!/bin/sh\n') 
    output.write('\n'.join(outputData)) 
    output.write('\n') 
    output.close() 
    os.chmod('{}'.format(output), 0o777) 
    os.system("screen -m -d bash -c ./" + output)  
    # Increment the counter 
    at += 1 

을 시도했지만 파이썬 파일을 실행하는 경우,이 오류는 다음 SH 파일에게 권한을 부여하고 보내기 이제 프로세스가 SH 파일에 분할 선을 작성해야

TypeError: coercing to Unicode: need string or buffer, file found 

을 보여 주었다 sh 파일을 실행할 화면에 저장합니다. 그 후에 모든 파일에 대해 동일한 단계에서 프로세스를 완료하십시오.

내 코드의 오류는 무엇입니까? 어떤 생각?

+0

오류의 ** 전체 추적 **을 포함 시키십시오. 그래서 어떤 행이 그 원인 일지 추측 할 필요가 없습니다. –

+0

@MartijnPieters 죄송합니다. 나는 Enter를 누르고 코멘트가 불완전하게 게시되었다. 그것은 당신의 대답과 같은 코드였습니다! 형식 – power

답변

1

os.chmod()은 파일 이름을 사용하지만 대신 파일 객체를 전달하려고합니다.

os.chmod(output.name, 0o777) 
os.system("screen -m -d bash -c ./" + output.name)  

또는 당신이 먼저 변수에 생성 파일 이름을 저장할 수 :

현재 output.name를 사용할 수 나 또한 여기에 상황에 맞는 관리자로 파일 객체를 사용

filename = '{0}{1}.sh'.format(outputBase, at) 
with open(filename, 'w') as output: 
    output.write('#!/bin/sh\n') 
    output.write('\n'.join(outputData)) 
    output.write('\n') 
os.chmod(filename, 0o777) 
os.system("screen -m -d bash -c ./{0}".format(filename))  

; with 문은 블록이 종료 될 때 파일을 닫습니다.

+0

안녕하세요,이 오류는 os.chmod (. '{}'형식 (output.name), 0o777) 에 ValueError에 파일 "1.py", 라인 (20)을, 나타났다 @ abualameer94 : 파이썬 2.6을 사용하고 있습니다. 나는 갱신 할 것이다. – abualameer94

+0

제로 길이 필드 이름 : –

관련 문제