0

This isnt a duplicate, as the original answer did not help into implementing it into my raw input command interpreter. Credit to original question/answer where due.

So I'm working on a DOS style Operating System in python and I was wondering how or even if it is possible to execute a command that would let me switch between directories like a regular command prompt would let you do.

Im not sure how well I'm explaining myself but basically I would have the same command as cd in terminal so I could read files imbedded in folders, change between folders etc. I don't need any sort of limit on how complex it is but i would prefer if it isn't too long.

I have yet to find a clear solution to this. in a best case scenario, this is all you would need to input

cd folder1
cd folderinfolder1

this would be inputted into a raw input or a input and then executed so im assuming it would require splits so it knows that before the space there is cd and after the space there is the filename, such as folder I am also have an issue with using os.listdir only showing files, and not any folders.

it would be used like this

directory=raw_input("enter cd followed with a directory")


This might be a bit much to ask, not even sure if it is possible

JROS
  • 71
  • 1
  • 2
  • 11

2 Answers2

1

import os

os.chdir('folder1')

or

os.chdir('folderinfolder1')

Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • that would be changing the directory overall. what im looking for is that you would be using a raw input to determine that you would want to run this command and at the same time know what directory to refrence – JROS Jan 23 '19 at 22:50
  • `if 'cd' in directory:` `desired_directory = directory.split(' ')[1]` `os.chdir('desired_directory')` – Reedinationer Jan 23 '19 at 22:53
  • Last question, is there an easy way to print the current directory – JROS Jan 23 '19 at 23:09
  • os.getcwd()... there are plenty of other functions you may be interested in at: https://docs.python.org/3/library/os.html – Reedinationer Jan 23 '19 at 23:11
  • Thank you so much! – JROS Jan 23 '19 at 23:13
  • The only problem i see with os.getcwd() is that it list the entire path, not just the current directory, shouldve specified that – JROS Jan 23 '19 at 23:16
  • try this: `full_path = os.getcwd()` `split_path = full_path.split('\\')` `current_folder = split_path[-1]` – Reedinationer Jan 23 '19 at 23:24
-1

I have done this except using C while creating a shell, in my case I used a library with the chdir function and it looked like this:

static void min_cmd_cd_av(char *av, char ***env)
{
    char    *tmp;

    if (av && 0 == chdir(av))
    {
        tmp = min_env_get_val(env, "PWD=");
        min_env_set_path(env, "OLDPWD=", tmp);
        free(tmp);
        tmp = (char *)ft_memalloc(4098 * sizeof(char *));
        getcwd(tmp, 4098);
        min_env_set_path(env, "PWD=", tmp);
        ft_strdel(&tmp);
    }
    else
        ft_printf("\e[91mERROR: Invalid Path/Permissions\e[96m\n");
}

int         min_cmd_cd(int ac, char **av, char ***env)
{
    char    *tmp;

    if (ac <= 1)
    {
        tmp = min_env_get_val(env, "HOME=");
        if (tmp)
        {
            min_cmd_cd_av(tmp, env);
            ft_strdel(&tmp);
        }
        else
            ft_printf("\e[91mERROR: Home variable not found!\e[96m\n");
    }
    else if (ac > 1)
    {
        min_shell_path(&av, env);
        min_cmd_cd_av(av[1], env);
    }
    return (0);
}

I used the shell environment to update the PWD and OLDPWD which allows you to implement that into your shell as well.

KR34T1V
  • 433
  • 2
  • 15