The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
New to python and needs some help
Discuss New to python and needs some help in the Python Programming forum on Dev Shed. New to python and needs some help Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 18th, 2012, 01:05 PM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 29
Time spent in forums: 5 h 39 m 44 sec
Reputation Power: 0
|
|
|
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.
|

July 18th, 2012, 01:46 PM
|
 |
Contributing User
|
|
|
|
|
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!
|

July 18th, 2012, 11:19 PM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 29
Time spent in forums: 5 h 39 m 44 sec
Reputation Power: 0
|
|
Quote: | 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.
|

July 19th, 2012, 12:01 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 1
Time spent in forums: 22 m 15 sec
Reputation 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.
|

July 31st, 2012, 02:49 PM
|
 |
Contributing User
|
|
|
|
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_()
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|