Guest User

Untitled

a guest
Mar 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. import ftplib
  2. import sys
  3. import os
  4. import re
  5.  
  6. ftpServer = '10.88.160.83'
  7. ftpUser = 'xxx'
  8. ftpPass = 'xxx'
  9.  
  10. def reportThis(filename, filecontent, ftppath="/"):
  11. f = open(filename,'w')
  12. f.write(filecontent)
  13. f.close()
  14. f = open(filename,'rb')
  15. s = ftplib.FTP(ftpServer, ftpUser, ftpPass) # Connect
  16. s.storbinary('STOR '+ftpPath+filename, f) # Send the file
  17. s.quit()
  18. f.close()
  19. os.remove(filename)
  20.  
  21. #insertLineAt(filename, lineNumber, line) // void
  22. def insertLinesAt(filename, lineNumber, lines):
  23. allLines=[];
  24. with open(filename, "r") as fi:
  25. allLines = fi.readlines()
  26. fi.close
  27.  
  28. for line in range(len(lines), 0, -1):
  29. allLines.insert(lineNumber+len(lines)-1, lines[line-1])
  30.  
  31. print(allLines)
  32. with open(filename, "w+") as fo:
  33. fo.writelines(allLines)
  34.  
  35. #getLines(filename, lineNumber, before, after) // string array
  36. def getLines(filename, lineNumber, before, after):
  37. output = []
  38. counter=0;
  39. with open(filename, "r") as fi:
  40. for line in fi:
  41. if(counter >= lineNumber-before and counter <= lineNumber+after):
  42. output.append(line)
  43. counter = counter+1
  44. return output
  45.  
  46. #editLine(filename, lineNumber, newString) // void
  47. def editLine(filename, lineNumber, newString):
  48. with open(filename, "r") as fi:
  49. allLines = fi.readlines()
  50.  
  51. allLines[lineNumber]=newString
  52.  
  53. with open(filename, "w+") as fo:
  54. fo.writelines(allLines)
  55.  
  56. #removeLine(filename, lineNumber) // void
  57. def removeLine(filename, lineNumber):
  58. with open(filename, "r") as fi:
  59. allLines = fi.readlines()
  60.  
  61. allLines.pop(lineNumber)
  62.  
  63. with open(filename, "w+") as fo:
  64. fo.writelines(allLines)
  65.  
  66. #findLines(filename, pattern) // array of lineNumbers
  67. def findLines(filename, pattern):
  68. output = {}
  69. counter=0;
  70. with open(filename, "r") as fi:
  71. for line in fi:
  72. if(re.search(pattern, line)):
  73. output[counter]=line
  74. counter = counter+1
  75. return output
  76.  
  77.  
  78. if __name__ == '__main__':
  79. with open("test.txt", "w+") as fi:
  80. fi.writelines(['0 blah\n', '1 blah2\n', '2 blah3\n'])
  81. fi.close
  82.  
  83. #insertLinesAt("test.txt", 0, ["xxx\n"])
  84. #insertLinesAt("test.txt", 2, ["halalao\n", "hlolal\n"])
  85.  
  86. #print(getLines("test.txt", 1, 1, 0))
  87.  
  88. #removeLine("test.txt", 0)
  89.  
  90. #editLine("test.txt", 0, "hovno\n")
  91.  
  92. print(findLines("test.txt", '.?[ ]blah[0-9]'))
Add Comment
Please, Sign In to add comment