How to create folders in python desktop

Asked

Viewed 1,328 times

1

I was hoping to make a python program that would create a folder on the desktop, so I did:

import subprocess
subprocess.call('mkdir Pasta_Teste', shell= True)

But the code created a folder inside the project folder, so I tried using a prompt command that opened the desktop, then creating the folder, tried this:

import subprocess

subprocess.call('cd Desktop', shell= True)
subprocess.call('mkdir Pastinha', shell= True)

And I got this mistake:

O sistema n�o pode encontrar o caminho especificado.

And right after that he created the folder, but in the project folder How can I make it create a folder on Desktop?

5 answers

7

Summary

Working with directories in the shell is different from working with directories from within a program - transplanting commands will not work.

Even so, there are specific things in the use of directories with programs that the Python only simplified after version 3.5 - in the full text I explain better. But for your specific problem, you can do:

from pathlib import Path

desktop = Path.home() / "Desktop"
pastinha = desktop.mkdir("Pastinha")

and, from there on, for any file operation in that folder - for example, create a "data.db" file inside:

arq = open(pastinha / "dados.db", "wt") 

Note that no "chdir" or equivalent was used - explain below.

Create a Python directory

First: call supprocess.call("mkdir pastinha") to create a folder is the same as calling a locksmith to open the door of your home when you are holding the key. Instead of putting the key in the door and turning, you wait until the key chain comes, picks up his tools, opens the door, and pays the fee for it.

That is why in this way mkdir is invoked as an external process of the operating system - the system has to create a new process in the table of processes, reserve the resources for the same, read from HD the file with the mkdir binary, and then terminate the process. All this usually happens very fast, but it uses a thousand to 10 thousand times more computational resources than simply calling the operating system to create a directory.

This can be done directly from Python using the module os: os.mkdir("Pastinha").

From Python 3.5 however, there is a new API for everything that is related to the file system, which is pathlib - it has a unified object to represent filenames or directories, which already has methods to open or read a file directly, or create a new directory underneath that one. With pathlib, the full code would be:

from pathlib import Path
desktop = Path.home() / "Desktop"
desktop.mkdir("Pastinha")

(the use of getpass.getuser to get the user’s name was suggested by Rafaeltuber in another reply, I would not remember doing that. But Anderson reminded (in the comments) that there is Path.home() that does this more directly, not knowing the user’s name)

Changing directory

You nay directory change! :-)

Here’s what makes your code not work. The question of "being in a directory" is something relative - the "working directory" is a variable of each process (program) that is running. Before calling the program in Python, if we are in a terminal (like the cmd), we change to the desired directory with cd pasta/pasta<enter>. When the program is called, its working directory is the one it was called "folder/folder" from - if you use any relative file path (that is, without starting with a "/" or " that goes back to the root of the file-system, this is where the actions happen). And this happens because it has an internal variable of the operating system, associated with your program, which says that the "current working directory" (CWD) is this "folder/folder".

If your own program makes a call to the operating system saying "I am changing the current program’s workbook", it is updated. If your program calls os.chdir("../outra_pasta"), the operating system understands that the "current working directory" (CWD) is now "folder/other_folder". Only when your show is over, and you’re back at the CMD, what’s the CWD of the CMD? Just the "folder/folder" - it did not move just because the child process, your Python script, changed the directory itself.

When you use a subprocess to call "chdir" the same thing happens. "chdir" is a command embedded in the shell. In the case of Windows the "cmd" itself. The operating system then creates a new process, runs a new CMD internally, as described above, this cmd calls the operating system to change own working directory for another - -and terminates. The execution of your Python program continues in the working directory it was in before.

However the story does not end there - even calling os.chdir(...), and by correctly changing the working directory of your program, this practice is not recommended in modern programs. That’s why, since the working directory is a state of the entire program, any function that changes it will change the directory also in the functions that called it, and any other place in the program - that is, it is not a "watertight" change. If the program is multi-threaded then the situation is even worse. In other words: in a more complex program that uses directory change, it does not give a part of the program to know which is the current directory - another function may have changed the directory.

The good practice then is that any and all actions in the file system use the absolute path - starting from the "/" directory, or, in the case of Windows, from the drive name: "C:/". Prior to pathlib this implied that programs that were guaranteed to work had to stay using os.path.join all the time - with Pathlib it becomes much simpler.

How to proceed to perform actions in a specific directory

In modern Python then, instead of trying to change the directory, the most correct It is simpler to store the directory where you want to do the operations in a variable, and for any operation - be it create a file, another directory, etc... use the operator / (the same used for division of numbers - it is re-signified for Pathlib objects) to create new objects Path:

desktop = pathlib.Path("/users/xxxx/Desktop")
minha_pasta = desktop.mkdir("Minha Pasta")
# Criar novo arquivo para scrita dentro dessa nova pasta:

with (minha_pasta / "meu_arquivo.txt").open("wt") as arq:
   # dentro deste bloco, "arq" é o arquivo
   # DEsktop/Minha Pasta/meu_arquivo.txt" aberto para escrita
   ....
  • 1

    About getting to the user’s Desktop, it could not be Path.home() / 'Desktop', which it uses as a basis os.path.expanduser()?

  • Ah - can also - better than using the getpass, which in that case does not even need to be imported

1

To create a folder inside the Desktop we should use the following logic:

  1. Specify the name of the folder we want to create on the Desktop;
  2. Access the Desktop;
  3. Check if the desired folder already exists inside the Desktop;
  4. If the desired folder does not exist inside the Desktop, create the said folder;
  5. Position yourself inside said folder.

With this logic we can implement the following code:

from os import mkdir, chdir, getcwd, path
import os.path

pas = input('Digite o nome da pasta: ')

cam = path.join(path.expanduser('~'), 'Desktop')
chdir(cam)

if os.path.isdir(pas):
    print(f'A pasta "{pas}" já existe em Desktop!')

else:
    mkdir(pas)
    cam2 = cam + '\\' + pas
    chdir(cam2)
    print(getcwd())

Note that when we execute the code, we must inform the name of the briefcase that we wish to create and put pressure on Enter.

From this moment the code will enter the respective user’s Desktop, check if the folder you want already exists in its dependencies and, if not, create the said folder and position itself inside it. If so, it displays a message that said folder already exists.

1

Take the system name of the user with getpass.

import subprocess
import getpass
usuario = getpass.getuser()
subprocess.call('mkdir C:\\Users\\' + usuario + '\\Desktop\\Pasta_Teste', shell= True)

edited removed "" by '' test and worked

  • The idea of taking the user name and using an absolute path is cool. But the failure happens even pq. it tries to change directory in a sub-process.

  • (I include your suggestion in my reply - so are all good practices and a complete example in one place)

  • Blz thank you and I’m new here over the hint I gave myself I tested here using inside the sub-process and it worked I hope he got it

-1


Your code is using subprocess.call('cd Desktop', shell= True) does not work because the subprocess.call() works as a single command only, so when Voce runs it with the cd, can be that it is indeed going to the directory you requested, but then you run a new one subprocess.call() to execute the mkdir, then at this time the mkdir works with a new command, that is, it is not in the folder you expect it to be in.

You can try to solve with os.chdir():

os.chdir("/desktop") # lembre-se de usar o caminho absoluto para a pasta
subprocess.call('mkdir Pasta_Teste2', shell= True)

I hope I’ve helped.

  • Yes 0 this will work for a simple script, but as I detailed in my reply, relying on directory change for the entire process is not the ideal solution.

-1

Pass the full path to the desktop... In this case it is looking for the folder: Desktop inside the folder of your project, and as it does not find it presents this error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.