July 18th, 2012, 01:05 PM
New to python and needs some help
Hi,guys I am new to python.Recently，I learn it by myself. I met a exercise as following can some body give a clue about how to accomplish it？ Thank you in advance.
Allow the user to edit an existing text file. You
can do this any way you wish. let the user edit line by line . Give users
the option to apply the changes (saving the file) or discard them (leaving the original
file intact), and also ensure the original file is preserved in case the program exits
abnormally during operation.
July 18th, 2012, 01:46 PM
This question is crazily open-ended.
Solution 1: Easiest to program, far and away the best:
Have the python program run emacs. emacs will take care of all the rest for you.
Solution 2: copy the filename.txt to filename.bak , load the text into a tkinter text box as a variable. Let tkinter handle the edits for you.
Solution 3: try to implement a subset of ed following the backups and all.
"anyway you want" conflicts with "line-by-line". What is the true assignment?
[/code] are essential for python code and Makefiles!
July 18th, 2012, 11:19 PM
Originally Posted by b49P23TIvg
Thank you very much.
July 19th, 2012, 12:01 AM
Hmmm. sound is good, although i'm also a coder probably new to python coding so it's not big issue, anyway it is good.
July 31st, 2012, 02:49 PM
Using one of the popular GUI toolkits available for Python might help:
explore PySide's QTextEdit multiline text entry box
click right mouse button in edit area for popup menu
for undo, cut, copy. paste, select All, etc.
the sample text is loaded using the file dialog widget
PySide is the official LGPL-licensed version of PyQT
you can downloaded the free Windows self-extracting installer
from PySide.QtCore import *
from PySide.QtGui import *
def __init__(self, parent=None):
# setGeometry(x_pos, y_pos, width, height)
self.setGeometry(100, 50, 500, 300)
self.edit = QTextEdit(self)
self.load_button = QPushButton(self)
self.load_button.setText('Load a text file')
# use grid layout manager
grid = QGridLayout(self)
# addWidget(QWidget, row, column, rowSpan, columnSpan)
grid.addWidget(self.load_button, 0, 0, 1, 1)
grid.addWidget(self.edit, 1, 0, 1, 2)
# bind the button clicked to action
# newer connect style used with version 4.5 or higher
load the selected filename and display in QTextEdit
caption = 'Open file'
# use current/working directory
directory = './'
filter_mask = "Python/Text files (*.py *.pyw *.txt)"
filename = QFileDialog.getOpenFileName(self,
caption, directory, filter_mask)
print(filename) # test
# use try/except to trap any errors
fh = None
fh = QFile(filename)
# check if file is loaded
if not fh.open(QIODevice.ReadOnly):
stream = QTextStream(fh)
# set the proper decoding
# load the text to self.edit
# optional show filename in title bar
except (IOError, OSError) as e:
QMessageBox.warning(self, "Load Error",
"Failed to load %s: %s" % (filename, e))
if fh is not None:
app = QApplication()
frame = MyFrame()
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25