#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    39
    Rep Power
    3

    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.

    Question:
    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.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    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]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    39
    Rep Power
    3
    Originally Posted by b49P23TIvg
    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?

    Thank you very much.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Hmmm


    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.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Using one of the popular GUI toolkits available for Python might help:
    [code=python]'''ps_TextEdit1.py
    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
    PySide-1.1.0qt474.win32-py2.7.exe
    or
    PySide-1.1.0qt474.win32-py3.2.exe
    from:
    http://developer.qt.nokia.com/wiki/PySide_Binaries_Windows
    '''

    from PySide.QtCore import *
    from PySide.QtGui import *

    class MyFrame(QWidget):
    def __init__(self, parent=None):
    QWidget.__init__(self, parent)
    # setGeometry(x_pos, y_pos, width, height)
    self.setGeometry(100, 50, 500, 300)
    self.setWindowTitle('QTextEdit() test')

    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)
    self.setLayout(grid)

    # bind the button clicked to action
    # newer connect style used with version 4.5 or higher
    self.load_button.clicked.connect(self.load_file)

    def load_file(self):
    """
    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)[0]
    print(filename) # test
    # use try/except to trap any errors
    fh = None
    try:
    fh = QFile(filename)
    # check if file is loaded
    if not fh.open(QIODevice.ReadOnly):
    raise IOError(str(fh.errorString()))
    stream = QTextStream(fh)
    # set the proper decoding
    stream.setCodec("UTF-8")
    # load the text to self.edit
    self.edit.setPlainText(stream.readAll())
    # optional show filename in title bar
    self.setWindowTitle(QFileInfo(filename).fileName())
    except (IOError, OSError) as e:
    QMessageBox.warning(self, "Load Error",
    "Failed to load %s: %s" % (filename, e))
    finally:
    if fh is not None:
    fh.close()


    app = QApplication([])
    frame = MyFrame()
    frame.show()
    app.exec_()

    [/code]
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo