Python noob menu


 
Thread Tools Search this Thread
Top Forums Programming Python noob menu
# 1  
Old 10-20-2013
Python noob menu

Hey guys, literally just starting off on python, just making a simple menu based system to read/write files and delete a file to get used to calling functions and working with the file system etc.
For some reason the code won't write to the file I have on my system, not sure what I'm doing wrong.

All help or constructive criticism about how I'm going about this would be much appreciated.

Code:
import os

def writer_func():

    file_targ=raw_input("Enter file path and name: ")
    texty=raw_input("Enter text: ")
    file_targ=str('file_targ')
    fob=open('file_targ','w')
    fob.write('texty')
    fob.close()
    print '%s' % texty

print "        Main Menu"
print "1) Write a file"
print "2) Read a file"
print "3) Delete a file"

main_menu=raw_input('Enter options [1-3] : ')

main_menu=int(main_menu)

if main_menu == 1:
    print ("You chose to write a file")
    writer_func()
    
elif main_menu == 2:
    print ("You chose to read a file")
    reader_func()
    
elif main_menu == 3:
    print ("You chose to delete a file")
    deleter
    
else:
    print ("Invalid choice")
    exit

# 2  
Old 10-20-2013
I wrote a couple of comments:
Quote:
Originally Posted by 3therk1ll
Hey guys, literally just starting off on python, just making a simple menu based system to read/write files and delete a file to get used to calling functions and working with the file system etc.
For some reason the code won't write to the file I have on my system, not sure what I'm doing wrong.

All help or constructive criticism about how I'm going about this would be much appreciated.

Code:
import os

def writer_func():

    file_targ=raw_input("Enter file path and name: ")
    texty=raw_input("Enter text: ")
    file_targ=str('file_targ')               # This resets what the user input into the variable file_targ two lines earlier to the string "file_targ"
    fob=open('file_targ','w')                # Here you are using the string "file_targ" instead of the variable file_targ
    fob.write('texty')                       # Here you are writing the string "texty" instead of what the user input in the variable texty
    fob.close()
    print '%s' % texty

print "        Main Menu"
print "1) Write a file"
print "2) Read a file"
print "3) Delete a file"

main_menu=raw_input('Enter options [1-3] : ')

main_menu=int(main_menu)

if main_menu == 1:
    print ("You chose to write a file")
    writer_func()
    
elif main_menu == 2:
    print ("You chose to read a file")
    reader_func()
    
elif main_menu == 3:
    print ("You chose to delete a file")
    deleter
    
else:
    print ("Invalid choice")
    exit

# 3  
Old 10-20-2013
Hi 3therk1ll...

I notice you are using a pre-version 3.x.x version of Python, 2.5.x to 2.7.x perhaps...

May I suggest you install a late version 3.x.x, as the syntax and function usages can
be much different, perhaps 3.3.x. (Note the latest alpha release is 3.4.0a3.)...

"print" is no longer a statement but a function for example and "raw_input" no longer
exists. These are but two examples...

Good luck with Python however as it is a superb language for general purpose use and
has an enormous number of libraries to use for just about every application one will
ever encounter...

EDIT:
Strange as it may seem however I have _dumped_ Python in preference to shell scripting...

My latest upload 4 weeks ago to code.activestate.com is here:-

http://code.activestate.com/recipes/...in=lang-python

Last edited by wisecracker; 10-20-2013 at 07:19 AM.. Reason: See above...
# 4  
Old 10-20-2013
Cheers mate, fixed it from that. Big help!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

3. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

4. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. Shell Programming and Scripting

Need help in create menu with 3 sub menu using the case command

hi all i am a newbie to this is there any examples on creating a main menu with 3 sub menu main menu -> option a , b and c a menu -> option 1 ,2 and 3 b menu -> option 1 ,2 c menu -> option 1 ,2 i am getting headache as my code kept getting unexpected EOF ---------- Post... (0 Replies)
Discussion started by: chercm
0 Replies

6. Shell Programming and Scripting

Menu with sub-menu options

Hi! I have created on script which is working fine with menu options and with a sub-menu. I want to enhance it by using sub-options under menu options. Like. option 1) will give the list only. option 1.1) should give the option to user to choose one file, whose content user wanna see. ... (3 Replies)
Discussion started by: sukhdip
3 Replies

7. Shell Programming and Scripting

Need help to change values in XML using Python? noob help pls!

Hello I am a noob in XML and Python. I am trying to do this for my MSc project about a network simulation and need some help.... I want to change the values shown below: <num_crash_failures>1</num_crash_failures> −<crash_failure_entry> <freeze_at_slot>0</freeze_at_slot>... (0 Replies)
Discussion started by: erhanasd
0 Replies

8. Shell Programming and Scripting

Menu in Menu script issue

Problem: I am trying to create a menu in a menu script and I am running into an issue with the calculator portion of the script. I am first presented with the ==Options Menu== which all 5 options working correctly. Now comes the fun part. I select option 1 which takes me to my ==Calculator... (1 Reply)
Discussion started by: iDdraig
1 Replies
Login or Register to Ask a Question