How to create a directory in Python inside a server?

1

Using the mkdir function, you can create a file in any directory within your computer's folders:

Import os
diretorio = "C:\Users\CRIACAO 2\Desktop\teste"
os.mkdir(diretorio)

But when I try to create it inside the address of a server, the program is not able to find the path indicated:

Import os
diretorio = "\servidor\ARABRINDES 1TB\Artes"
os.mkdir(diretorio)
  

FileNotFoundError: [WinError 3] The system can not find the path specified: '\ server \ ARABRINDES 1TB \ Arts \ andrei'

How can I resolve this problem?

    
asked by anonymous 18.07.2018 / 22:13

2 answers

3

Your problem is that you are using a backslash only, and server paths start with two backslashes \ ...

Explaining better, Python interprets strings that have special backslashes. For example, '\t' is the tab, '\n' is the line break and '\' is a single bar!

You can see that this is the case through the error message:

FileNotFoundError: [WinError 3] O sistema não pode encontrar o caminho especificado: 
'\servidor\ARABRINDES 1TB\Artes\andrei'

To solve, use four bars, that is, two for each bar:

diretorio = "\\servidor\ARABRINDES 1TB\Artes"

Or even better, use raw strings whenever you type a path:

diretorio = r"\servidor\ARABRINDES 1TB\Artes"

Placing this r before the string causes python to not process special characters within it. It comes out exactly as it is written!

Read the documentation here: link

    
19.07.2018 / 14:27
2

I had a similar problem, this error happened because you are running the command for a relative path and not an absolute path . The correct would be to pass through the server's entire path.

I hope I have helped.

    
19.07.2018 / 01:04