Ok, so I am basically trying to f.write a specific set of code if nothing in the file exists already. This is the code I'm using:
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)
Can anybody explain why this does not work and if it is possible to scan a file and then write specific information into it?
Found a way to make it work with:
with open('Pass.html', 'a') as f:
if os.path.getsize('C:\Python26\Pass.html') != 0 :
print('butters')
else:
f.write(bigtable)
Once you've opened the file in append mode (as in your edit), use tell()
to see if the file was empty (or nonexistent) before.