How to create a Python directory inside a server?

Asked

Viewed 1,183 times

1

Using the function mkdir it is possible to create a file in any directory inside the folders of the computer:

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 indicated path:

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

Filenotfounderror: [Winerror 3] The system cannot find the specified path: 'ARABRINDES 1TB Arts server Andrei'

How can I solve this problem?

2 answers

2


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

Explaining better, Python interprets strings that have contrabars in a special way. 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, ie two for each bar:

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

Or better yet, use raw strings whenever you write a path:

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

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

Read the documentation here: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

  • Thank you very much! The mistake was exactly that and I managed to solve it.

1

I had a similar problem, this error happened because it is running the command for a relative path and not a absolute path. The correct one would be to pass the whole server path by parameter.

I hope I’ve helped.

  • Not the case, the way \\servidor\ARABRINDES 1TB\Artes is absolute - includes the server name, sharing and all the way to reach the folder.

  • Thanks for the correction, I ended up confusing the problem I had.

Browser other questions tagged

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