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

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Cool Edit a txt file using an option list in python


    Hey everybody i am new to python and want to know how do i create a list a b c d and use that option to edit a text file in python
    I have a test.txt file I want to target. I made a list with square brackets.
    A
    B
    C
    D
    I want to change one line in test.txt by picking option
    A, B, C, or D. So on the only one lone in the text file could only be
    A, B, C, or D
    If anyone could offer me some help
    I would appreciate it. Thanks again.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    I'm not an experienced Python programmer but I believe you have to do this.

    Code:
    n = input()
    switch(n) {
       case A:
           f = open(".txtFile", 'w')
           f.readline()
           f.write('whatever')
           f.close()
           break;
         }
    Then you just repeat that 4 times, for A, B , C, D. Like I said I'm a new python but from other past languages thats what you have to do.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Nice try. Python hasn't got a switch statement and Guido kicks everyone who suggests he update his language with one.

    Nor can you read from a file opened with write access.

    >>> open('/tmp/a','w').readline()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    io.UnsupportedOperation: not readable


    However, bbesase pretended to understand the question. Maybe bbeasase did. I don't.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Cool My Code


    Here i my code so far,

    #-------------------------------------------------------------------------------
    # Name: module1
    # Purpose:
    #
    # Author: Will
    #
    # Created: 08/05/2013
    # Copyright: (c) Will 2013
    # Licence: <wchristner>

    #try:
    # number=int(raw_input('input:'))
    #except ValueError:
    # print "Not a number"
    #
    #with open ("test.txt", "w") as pfile:
    # prile.write("Optional")

    #-------------------------------------------------------------------------------
    with open ("D:\Autorun_Ghost.cmd", "w") as pfile:
    pfile.write("\programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88xpTab.GHO,dst=1 -IR -SURE")



    print "Choose an Image"
    print "======================================="
    print "1)Ubuntu"
    print "2)Windows Embedded Standard 2009 XP"
    print "3)Windows Embedded Standard 7"
    print "4)Windows POSReady Embedded Standard 2009"
    print "5)Windows XP Tablet Edition"
    print "6)Windows 7 Pro"
    print "7)Windows 7 Ultimate"
    print "======================================="
    print "press q to quit"



    the options should edit the .cmd file
    \programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88ubun1.GHO,dst=1 -IR -SURE

    \programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88WES.GHO,dst=1 -IR -SURE

    \programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88wes7.GHO,dst=1 -IR -SURE

    \programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88POSR.GHO,dst=1 -IR -SURE

    \programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88xpTab.GHO,dst=1 -IR -SURE

    \programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88win7.GHO,dst=1 -IR -SURE

    \programs\ghost11\ghost32.exe -clone,mode=restore,src=x:\88w7ult.GHO,dst=1 -IR -SURE

    Any ideas on how to put this togeather?
    Any help or tutorial would be greatly appreicated, I have learned a lot but I still have a long way to go.
    Thanks Everybody.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    # how's this for whacko?
    
    import sys
    import collections
    
    print("Choose an Image")
    print("=======================================")
    print("1)Ubuntu")
    print("2)Windows Embedded Standard 2009 XP")
    print("3)Windows Embedded Standard 7")
    print("4)Windows POSReady Embedded Standard 2009")
    print("5)Windows XP Tablet Edition")
    print("6)Windows 7 Pro")
    print("7)Windows 7 Ultimate")
    print("=======================================")
    print("press q to quit")
    
    class c:
        def __init__(self,a):
            self.a = a
        def __call__(self,*args):
            return self.a
    
    d = collections.defaultdict(sys.exit)
    d.update({
        '1':c('\\programs\\ghost11\\ghost32.exe -clone),mode=restore),src=x:\88ubun1.GHO),dst=1 -IR -SURE'),
        '2':c('\\programs\\ghost11\\ghost32.exe -clone),mode=restore),src=x:\88WES.GHO),dst=1 -IR -SURE'),
        '3':c('\\programs\\ghost11\\ghost32.exe -clone),mode=restore),src=x:\88wes7.GHO),dst=1 -IR -SURE'),
        '4':c('\\programs\\ghost11\\ghost32.exe -clone),mode=restore),src=x:\88POSR.GHO),dst=1 -IR -SURE'),
        '5':c('\\programs\\ghost11\\ghost32.exe -clone),mode=restore),src=x:\88xpTab.GHO),dst=1 -IR -SURE'),
        '6':c('\\programs\\ghost11\\ghost32.exe -clone),mode=restore),src=x:\88win7.GHO),dst=1 -IR -SURE'),
        '7':c('\\programs\\ghost11\\ghost32.exe -clone),mode=restore),src=x:\88w7ult.GHO),dst=1 -IR -SURE'),
        })
    
    command = d[sys.stdin.readline().strip()](0)
    print(command)
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Drive D:\


    I don't know if it matters but all os ghost images are on a 16GB flash D:\ nothing id done on the C: Drive
    also gor this error


    Message File Name Line Position
    SyntaxError
    invalid syntax (8800 list.py, line 39) D:\Portable Python 2.7.3.2\python projects\8800 list.py 39 22
    line 39 def_init_(self,a):
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Originally Posted by me, from a recent thread
    Compilers detect syntax errors after the mistake in the program source. That's because the parser is busy looking ahead trying to make sense of your program in a way the programmer didn't expect---but is valid syntax.
    Most of us don't know about your
    list.py
    or "8800 list.py" program.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Most of us don't know about your
    list.py
    or "8800 list.py" program.
    yea i know, I work for a tablet manufactoring company, so we have vaious operating systems on the 8800 tablet we sell.

    thanks for your help,

    one more question though, if the syntax is valid then I can just turn this into an exe and run it? if so cool, if there is somthing else i need to do then, well...thats cool too.
    thanks again
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    I don't know if your syntax is correct. You said it's wrong, and you haven't shown your program. Also, I see no reason you shouldn't need to understand python and your operating system and executable programs a little bit.

    Study and practice this! (link here)
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    39
    Rep Power
    3
    I'm not sure why you need to use Python; just use the CHOICE command in the batch file (.cmd).

    If there is a reason for Python I would use the subprocess module to run the command directly rather than edit another file.

IMN logo majestic logo threadwatch logo seochat tools logo