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

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Using Python as a Linux shell ?


    Hi, I'm new to python.

    I also tried to search the forum, but the search button did nothing when I clicked it.

    I want to use Python as my shell, I cant find any commands like "ls" "cd"

    How do you use Python as a default shell and have normal commands ?

    Thanks,
    Areal
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    (Assuming python is installed on your system)

    1. Launch a terminal and type "python".
    2. This will not work if your environment path doesn't have path to python executable.
    3. After you type python and hit enter, you can see the python shell's prompt (typically like this - ">>>")
    4. And, to be able to use python shell to list files and change directory, you need to use the appropriate commands (since python is different from bash/ksh/csh/zsh, the commands too will be different)
    5. Try this:

    Code:
    >>> import os
    >>>
    >>> # Equivalent of "pwd"
    >>> os.getcwd()
    >>> '/home/user/python'
    >>> 
    >>> # Equivalend of "cd"
    >>> os.chdir('/home/user1/mydir')
    >>>
    >>> # Equivalent of "ls"
    >>> os.listdir()
    ['file1', 'file2', 'file3']
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    you could also use the subprocess module to run programs you have on your computers.

IMN logo majestic logo threadwatch logo seochat tools logo