2017-10-09 13 views
0

기본적으로 파일에 아무것도없는 경우 특정 세트의 코드를 f.write하려고합니다. 이것은 내가 사용하고 코드입니다 :파일이 비어 있지 않은지 검사하고 파일이 f.write인지 확인합니다.

import sys 
import os 
from string import * 

userType = raw_input("Enter text: ") 

bigtable = '''<html> 
<head> 
<style> 
table, th, td { 
    border: 1px solid black; 
    border-collapse: collapse; 
} 
th, td { 
    padding: 5px; 
    text-align: left;  
} 
</style> 
</head> 
    <body> 
    <table style="width:50%"> 
     <tr> 
      <th>Server</th> 
      <th>Address</th> 
      <th>Name</th> 
      <th>Address2</th> 
     </tr>''' 

if userType == 'file -n' or userType == 'file --nice': 

    with open('Pass.html', 'r') as f: 

     if str(f) != 0 : 
     print('butters') 
     else: 
     f.write(bigtable) 

이 작동하지 않는 이유를 아무도 설명 할 수 있으며 파일을 스캔 한 다음에 특정 정보를 기록 할 수 있다면?

는 함께 작동하도록하는 방법을 찾았

당신이 (당신의 편집과 같이) 추가 모드에서 파일을 연 후
with open('Pass.html', 'a') as f: 

     if os.path.getsize('C:\Python26\Pass.html') != 0 : 
      print('butters') 
     else: 
      f.write(bigtable) 
+0

한 가지만 열어 읽은 파일에 쓸 수 없습니다. 또한 str()을 int와 비교합니다. 그러나 "효과가 없다"는 것은 무엇을 의미합니까? –

+0

는 코드 자체와 관련이 없으므로 기본적으로 왜 내가 잘못했는지는 모르지만 코드가 문자 그대로 잘못되었다는 사실 외에는 문자열을 int와 비교한다는 사실이 문제입니다 – Cry2Senpai

답변

1

파일이 비어 (또는 존재하지 않는)이었다 있는지 tell()를 사용 전에.