有没有简单的方法来获取子文件夹的路径?

时间:2020-03-29 01:35:58

标签: path operating-system

因此,我的程序为用户提供了创建文件夹和子文件夹的权限... 到目前为止,我设法使其在默认目录中创建了该文件夹,但是当我尝试创建子文件夹时,这对我来说变得很复杂...我已经尝试解决了几个小时,但是我不能。 如果有人可以帮助我,那将是非常愚蠢的事情!!! 他们是对代码的注释,以帮助更好地理解。 谢谢

import os
from os import listdir
from pathlib import Path

def display_menu(): #here is the menu for our display
    print("The File Manager program")
    print()
    print("COMMAND MENU")
    print("List - list all folders/subfolders/files")
    print("add - add a folder/subfolder/file")
    print("exit - Exit program")
    print()

def add(fm_list):
    msg = input("What do you want to create?: \n Insert 1 for Folder "
                "\n Insert 2 for Subfolder."
                "\n Insert 3 to choose a different location than DEFULT ")
    if msg == "1":
        folder = input("Name of the folder: ")
        fm_list.append(folder)
        os.chdir('F:\\Test')  # this path is our test path where folders and subfolders and files will be stored
        os.mkdir(folder)
        choice = input("Do you want to add a subfolder to this folder?: (y/n): ")
        if choice == "y":
            #In below, I have difficulties to make a path for the subfolder
            #I have tried all I know but nothing...
            #Instead of making the subfolder in the appropriate folder
            #it creates a folder
            subfolder = input("Name of the subfolder: ")
            fm_list.append(subfolder)
            os.chdir('F:\\Test\\')#HERE
            os.mkdir(subfolder)
            print(folder + " was added to the list of folder.")
            print(subfolder + " was added to " + folder)
        else:
            os.chdir('F:\\Test')  # this path is our test path where folders and subfolders and files will be stored
            #os.mkdir(folder)
            #os.chdir(folder)
            print(folder + " was added to the list of folder.")
            print("Your folder path is : " + str(Path.cwd()))  
def main():
    listdir(my_path)
    fm_list =[]
    display_menu()
    while True:
        command = input("Command: ")
        if command.lower() == "list":
            list(fm_list)
        elif command.lower() == "add":
            add(fm_list)
        elif command.lower() == "copy":
            copy(fm_list)
        elif command.lower() == "exit":
            break
        else:
            print("Not a valid command.\n")


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

在阅读您提供的代码后,我注意到您在同一级别创建了文件夹和子文件夹。

要创建一个文件夹,假设您的沙箱为“ F:\ Test”,请编写以下代码:

 os.chdir('F:\\Test') # you changed your current directory to 'F:\\Test' (chdir means change directory equivalent to cd in linux shell)
 os.mkdir(folder) # you created a folder

 ... # after a few lines

 os.chdir('F:\\Test') # you're still in the same directory
 os.mkdir(subfolder) # folder and subfolder are thus created under the same directory

由于在创建子文件夹之前没有更改当前目录,因此最终会得到两个处于同一级别的文件夹。这是我的建议。

子文件夹必须位于先前在'F:\ Test'下创建的文件夹内。

 os.chdir('F:\\Test') #changed the current directory to 'F:\Test'
 folder = 'Folder' # the name of the folder
 os.mkdir(folder)  # creates a folder inside 'F:\Test' and you get 'F:\Test\Folder'

 subfolder = 'Subfolder'

 # You need to concatenate 'F:\\Test' with the parent folder of your subfolder. I assume that you picked the previously created folder.

 os.chdir('F:\\Test\\' + folder) # changed the current directory to 'F:\Test\Folder'
 os.mkdir(subfolder) # creates a folder inside 'F:\Test\Folder' and you get 'F:\Test\Folder\Subfolder'
相关问题