Results 1 to 4 of 4

Thread: Find and replace in python 3.2?

  1. #1
    Join Date
    Feb 2013
    Location
    UK, West Yorkshire
    Beans
    62
    Distro
    Xubuntu 14.04 Trusty Tahr

    Question Find and replace in python 3.2?

    Hi, I am quite new to python and was just recently writing a python 3.2 script for installing all my favourite programs on a fresh install of ubuntu. I have only just learned about if statements and wondered if there was a way I can use them to write a script to upgrade from earlier versions of ubuntu, e.g upgrade from precise to quantal.

    For example, I want the script to take input from the user about which version they are using and the version they are going to upgrade to. Then look for the first input in the
    Code:
    /etc/apt/sources.list
    and replace it with the second input. I am guessing that if statements would be used. I cannot find how to do this even with a quick google.

    Any help would me much appreciated.

    Thanks very much in advance.

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Find and replace in python 3.2?

    python is a little overkill for that, a simple sed -e "s/old/new/" file would do that too, also you should use update-manager -d or do-release-update for that

    but if you want to do it in python the things you need is:
    raw_input
    some string comparisons
    open
    .read method of files
    .replace method of strings
    .write method of files

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,333
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Find and replace in python 3.2?

    agreed about python being an overkill and that using upgrade paths provided by the system is how it's supposed to be done.

    also asking user about his release doesn't make much sense, what if the answer is wrong? you can probe the system directly, eg if you want to get codename
    Code:
    lsb_release -sc
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Feb 2013
    Location
    UK, West Yorkshire
    Beans
    62
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: [SOLVED] Find and replace in python 3.2?

    Ok, I will just write myself a simple bash script then. Thanks for the help.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •