Generating more than 1 directory with a single os.mkdir function (Python)
instead of os.mkdir
use os.makedirs
, should work.
simple example:
os.makedirs("brand/new/directory")
should create the directories: brand
, new
and directory
from https://docs.python.org/3/library/os.html#os.mkdirs :
Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.
Why is os.mkdir() returning error when it shouldn't (beginner question)?
Don't do any checks. If you need the directory, create it. If it already exists, os.mkdir
will raise an exception that you can catch and ignore.
def init_files():
try:
os.mkdir('Assets')
except FileExistsError:
pass
The end result of this function is that Assets
will exist, whether or not it previously did. (Ignoring that fact that someone might delete it after you create it, but there's nothing sensible you can do about that.)
Doing otherwise exposes you to a race condition. You can check if the directory does not exist however you like, but it's always possible someone else could create the directory before your code executes os.mkdir
, in which case you have to be prepared to catch the exception anyway.
How can I make a directory with Python using default permissions?
You already are getting the same permissions you'd get with the shell's mkdir
.
With the shell mkdir:
For each dir operand, the mkdir utility shall perform actions equivalent to the mkdir() function defined in the System Interfaces volume of IEEE Std 1003.1-2001, called with the following arguments:
The dir operand is used as the path argument.
The value of the bitwise-inclusive OR of S_IRWXU, S_IRWXG, and S_IRWXO is used as the mode argument. (If the -m option is specified, the mode option-argument overrides this default.)
Or, more readably (from the BSD manpage):
... creates the directories named as operands, in the order specified, using mode rwxrwxrwx (0777) as modified by the current umask(2).
Python's os.mkdir
does the exact same thing:
... [t]he default mode is 0777... the current umask value is first masked out.
Python in fact calls the exact same POSIX mkdir function mentioned in the shell documentation with the exact same arguments. That function is defined as:
The file permission bits of the new directory shall be initialized from mode. These file permission bits of the mode argument shall be modified by the process' file creation mask.
Or, more readably, from the FreeBSD/OS X manpage:
The directory path is created with the access permissions specified by mode and restricted by the umask(2) of the calling process.
If you're on a non-POSIX platform like Windows, Python tries to emulate POSIX behavior, even if the native shell has a command called mkdir
that works differently. Mainly this is because the primary such shell is Windows, which has an mkdir
that's a synonym for md, and the details of what it does as far as permissions aren't even documented.
Python: How to create the folder in windows using 'os.mkdir()' with write permission?
Using os.path.join() helped me resolve the 'Persmission Denied' Error.
import paramiko
import time
file = ["a.txt","b.txt"]
remotePath = "SFTP/SERVER/PATH"
os.mkdir("./Jars")
os.chdir("./Jars")
getCurrDir = os.getcwd()
localPath = os.path.join(getCurrDir, file)
#SFTP Connect
ssh = paramiko.SSHClient()
ssh.connect(hostname=fromHost, port=fromPort, username=fromUsername, password=fromPassword)
sftp = ssh.open_sftp()
sftp.get(remotePath, localPath)
Python OS Library: mkdir and chdir in the same line?
You can define your own wrapper around the two functions:
import os
def a(b):
os.mkdir(b)
os.chdir(b)
Now, whenever you want to create a new directory and change to that directory, use
a(directory)
Also, if you'd really like to achieve that in one line, use
os.mkdir(you_are_welcome); os.chdir(you_are_welcome)
Related Topics
Hexadecimal String to Byte Array in Python
How to Check If a Float Value Is a Whole Number
Why Do I Get a Syntaxerror for a Unicode Escape in My File Path
Execute Code When Django Starts Once Only
Unicode (Utf-8) Reading and Writing to Files in Python
Importerror: No Module Named 'Encodings'
I Need to Securely Store a Username and Password in Python, What Are My Options
Differences Between Distribute, Distutils, Setuptools and Distutils2
Removing Emojis from a String in Python
Create Nice Column Output in Python
How to Select Rows in a Dataframe Between Two Values, in Python Pandas
Activate a Virtualenv with a Python Script
Adding Days to a Date in Python