File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Jython/Python and the fly likes python Notebook: Changing the written file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "python Notebook: Changing the written file" Watch "python Notebook: Changing the written file" New topic

python Notebook: Changing the written file

daniel kidanee

Joined: Feb 23, 2014
Posts: 19
please help me guys see this problem. The tool I am trying to submit my task is called ,a self study environment, and it sometimes makes mistakes when it checks your code. My code seems to work fine for the first round but I don't understand why it is not printing when you select 1 for the second round, it seems to capture the change of the file name.
Also the other continuous project, the notebook, has relied on user actions in the sense that it would have broken down if the user had decided to read the file without writing anything to it. In this exercise we fix this, and add the possiblity of changing the used notebook file while the program is running.

First of all, make the program start by checking if there is a file "notebook.txt" and create one if there is none. If this has to be done, also inform the user with the warning "No default notebook was found, created one.".

When this feature works, add a fourth selection to the notebook, "(4) Change the notebook". If the user selects this option, the user is prompted for a new file "Give the name of the new file: ". If there is an existing file, it is opened and loaded into the notebook program, while the old notebook file is closed. If the new notebook file does not exist, the system informs the user "No notebook with that name detected, created one." and makes a new file. Also add a note of the used notebook file to the main menu, "Now using file [filename]".
The correct output should look like:

my code

my code's output:

Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 602

OK as a beginning, you have really mucked up your text fiile.

You want to make sure:

a) all tabs are spaces (and your editor puts them in)
2) placing blank lines in control blocks, finishes the control block.
C) Never, Never, Never write this much code without testing - my limit is 10 lines of code in java - 3 in python.
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

I fear you open the file in one mode, then you never close. It could be that the file remains in the incorrect mode, so it either can't read if it is first opened as 'w' or it can't write when opened as 'r'. You should either open the file for both modes ('r+') (perhaps more safely, open the file for read and append ('a+') so you do not erase any data in the file when you open it), or you need to open it in the correct single mode, do the work, and close it so it is available to be opened in the correct mode for the next operation. The first method would probably be more efficient, but keeps the file locked while your application runs (for OSes that care about file locks), while the second option allows external applications to open the file as well (for example, you could open a text editor to view the file to ensure the file is being written to) and is a little easier to do the reading operations.

Also note that if you are in a read/write or read/append mode and you want to display the contents of the file you should seek to the start of the file - otherwise you will be reading only from the end of the last write (which may be the end of the file). Finally, you should flush the file whenever it is important to get the data to disk (i.e. before closing, switching files, or reading from the current file).

And don't forget to close the current file before opening a new one! Otherwise you would have resource leaks that could hurt you in the long run.


I agree. Here's the link:
subject: python Notebook: Changing the written file
It's not a secret anymore!