Search code examples
pythonshutilos.path

Move files to folder made by user input with the same name


I have a directory containing the following:

name.xlsx

I use the following code to have a user input names for the folder(s) they would like to create in that directory.

dirPath = ‘dir_path’

naFold = input(“Enter name: ”)

lst = naFold.split(“, “)

for names in lst:
    pa = os.path.join(dirPath, names)
    os.mkdir(pa)

Say a user inputs ‘name’ …

The directory will now contain the following:

name (folder created)

name.xlsx

I am trying to figure out a way to then move the ‘name.xlsx’ into the created folder.

Is this possible using shutil and or os?

The following code I tried works for moving a test file named ‘test.txt’ to a test folder named ‘test’ in the same directory …

path = ‘E:\\foldA\\test\\test\\test.test’

os.rename(‘E:\\foldA\\test\\test.test’, path)
os.replace(‘E:\\foldA\\test\\test.test’, path)
shutil.move(‘E:\\foldA\\test\\test.test’, path)

However, I am unsure of how to set the paths to the created folder from user input.

Also, the .xlsx will not be the same for each instance as it is also created by the user input; therefore, I’d also need to add that to the end after the user input for the folder.

Ex:

path = ‘E:\\foldA\\test\\’ + (folder created from user input) + ‘\\’ + (file .xlsx created from user input)

The .xlsx is created using the code below … Essentially, it uses the “name.xlsx” file in the folder already and copies it for each input while renaming it that input.

targetFile = r’file path of “name.xlsx”’

for i in lst:
    shutil.copy(‘file path of “name.xlsx”’, ‘file path of “name.xlsx” modified using {}’.format(i))

os.remove(targetFile)

Example of {} modification line:

shutil.copy(‘E:\\foldA\\test\\’ + (folder created from user input) + ‘\\’ + (file .xlsx created from user input), ‘‘E:\\foldA\\test\\’ + (folder created from user input) + ‘\\{}.xlsx’.format(i))

Basically, since the user input in the example at the beginning was “name” … the ‘name.xlsx’ file will in a sense stay ‘name.xlsx’… it is truly being copied with the input as it’s name and the os.remove deletes the original ‘name.xlsx’ file … so if the input was “name2” the ‘name.xlsx’ file would be copied as ‘name2.xlsx’ and the ‘name.xlsx’ file would be deleted.

So in total the code that I’m working with is:

dirPath = ‘dir_path’

naFold = input(“Enter name: ”)

lst = naFold.split(“, “)

for names in lst:
    pa = os.path.join(dirPath, names)
    os.mkdir(pa)

targetFile = r’file path of “name.xlsx”’

for i in lst:
    shutil.copy(‘file path of “name.xlsx”’, ‘file path of “name.xlsx” modified using {}’.format(i))

os.remove(targetFile)

… And I need to add the section that moves the .xlsx file to the folder with the same name.

Thanks!


Solution

  • Solved by : @Azgolar05

    1. ⁠The source folder can have multiple xlsx files.
    2. ⁠create new subfolder for each xlsx file. The folder name is the name of each xlsx file.
    3. ⁠Move every xlsx file from the source folder into the folder with the same name as the xlsx file.

    If that is the case, then the following should solve it:

    ​for stu in lst: 
        shutil.move(‘E:\\foldA\\test\\{}.xlsx’.format(stu),‘E:\\foldA\\{}\\{}.xlsx’.format(stu, stu))
    

    The code assumes that the name of the source folder will never be changed.