Skip to content

Instantly share code, notes, and snippets.

@dizzi
Created September 8, 2009 14:51
Show Gist options
  • Select an option

  • Save dizzi/182980 to your computer and use it in GitHub Desktop.

Select an option

Save dizzi/182980 to your computer and use it in GitHub Desktop.
import ftplib
import sys
import os
import re
ftpServer = '10.88.160.83'
ftpUser = 'xxx'
ftpPass = 'xxx'
def reportThis(filename, filecontent, ftppath="/"):
f = open(filename,'w')
f.write(filecontent)
f.close()
f = open(filename,'rb')
s = ftplib.FTP(ftpServer, ftpUser, ftpPass) # Connect
s.storbinary('STOR '+ftpPath+filename, f) # Send the file
s.quit()
f.close()
os.remove(filename)
#insertLineAt(filename, lineNumber, line) // void
def insertLinesAt(filename, lineNumber, lines):
allLines=[];
with open(filename, "r") as fi:
allLines = fi.readlines()
fi.close
for line in range(len(lines), 0, -1):
allLines.insert(lineNumber+len(lines)-1, lines[line-1])
print(allLines)
with open(filename, "w+") as fo:
fo.writelines(allLines)
#getLines(filename, lineNumber, before, after) // string array
def getLines(filename, lineNumber, before, after):
output = []
counter=0;
with open(filename, "r") as fi:
for line in fi:
if(counter >= lineNumber-before and counter <= lineNumber+after):
output.append(line)
counter = counter+1
return output
#editLine(filename, lineNumber, newString) // void
def editLine(filename, lineNumber, newString):
with open(filename, "r") as fi:
allLines = fi.readlines()
allLines[lineNumber]=newString
with open(filename, "w+") as fo:
fo.writelines(allLines)
#removeLine(filename, lineNumber) // void
def removeLine(filename, lineNumber):
with open(filename, "r") as fi:
allLines = fi.readlines()
allLines.pop(lineNumber)
with open(filename, "w+") as fo:
fo.writelines(allLines)
#findLines(filename, pattern) // array of lineNumbers
def findLines(filename, pattern):
output = {}
counter=0;
with open(filename, "r") as fi:
for line in fi:
if(re.search(pattern, line)):
output[counter]=line
counter = counter+1
return output
if __name__ == '__main__':
with open("test.txt", "w+") as fi:
fi.writelines(['0 blah\n', '1 blah2\n', '2 blah3\n'])
fi.close
#insertLinesAt("test.txt", 0, ["xxx\n"])
#insertLinesAt("test.txt", 2, ["halalao\n", "hlolal\n"])
#print(getLines("test.txt", 1, 1, 0))
#removeLine("test.txt", 0)
#editLine("test.txt", 0, "hovno\n")
print(findLines("test.txt", '.?[ ]blah[0-9]'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment