-
-
Save keithweaver/562d3caa8650eefe7f84fa074e9ca949 to your computer and use it in GitHub Desktop.
import os | |
def createFolder(directory): | |
try: | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
except OSError: | |
print ('Error: Creating directory. ' + directory) | |
# Example | |
createFolder('./data/') | |
# Creates a folder in the current directory called data |
Thank you so much
Thank you!
If someone is getting a unicode error when trying to create a folder in a directory (C\Users\etc...) just duplicate all backslashes like this (C\Users\etc...)
Saved me from writing 600+ new directories.
Thanks
good!
I wonder how decide directory name?
Isn't this all already done by
import os
os.makedirs(directory, exist_ok = True)
? (thanks @EricHedengren for correction below! ;)
exist ok = True
checks for existence and makes dir created if not existing
OSErrors from the os package will do the error messaging ...
hjoon0510 I am working on learning Python myself but as to selecting the NAME. I import the "socket", then set a variable to hostname = socket.gethostname(). I can then call that variable to name my file or directory. Again I am trying to learn also so there may be a better way to do it but here is the code I am playing with to learn.
import wmi
import datetime
import sys
import os
import platform
import getpass
import socket
import ifaddr
from psutil import virtual_memory
ps = platform.system()
pr = platform.release()
build = platform.version()
cdir = os.getcwd()
mem = virtual_memory()
username = getpass.getuser()
hostname = socket.gethostname()
adapters = ifaddr.get_adapters()
mType = platform.machine()
pType = platform.processor()
currentDT = datetime.datetime.now()
def createFolder(directory):
try:
if not os.path.exists(hostname):
os.makedirs(hostname)
except OSError:
return ('Error: Creating directory. ' + hostname)
createFolder('./hostname/')
sys.stdout = open("%s %s" % (hostname, (currentDT.strftime("%m%d%Y %H%M%S"))) + ".txt", "w+")
HDs = wmi.WMI()
for hdSerialnum in HDs.Win32_PhysicalMedia():
print(hdSerialnum.Tag.strip("\.\PHYSICAL"), hdSerialnum.SerialNumber)
print('')
print("Installed OS: " + ps + '\n' + "OS Version: " + pr + '\n' + "OS Build: " + build + '\n' +
"Current Dir: " + cdir + '\n' + "Machine Type: " + mType + '\n' + "Processor: " +
pType + '\n' + "RAM Total: " + str(mem.total) + '\n' + "Current User Name: "
+ username + '\n' + "Computer Name: " + hostname + '\n')
for adapter in adapters:
print(adapter.nice_name)
for ip in adapter.ips:
print(" %s/%s" % (ip.ip, ip.network_prefix))
sys.stdout.close()
thank you so much!
This is wonderful!
Thanks alot!
thank u
Thanks
Thank you! :)
Thank BRO!!!
you can change your directory by os.chdir
Thank you
Thank you very much..... keithweaver
if anybody facing issue while creating the folder, followed with an error message like this
def createFolder(D:\Sampath\web_scrapper\temp):
^
SyntaxError: unexpected character after line continuation character
please just duplicate with a backslash for the directory you want to create. Here I want to create a Temp folder in web_scrapper folder
createFolder('D:\Sampath\PAMM_web_scrapper/temp/')
Isn't this all already done by
import os os.mkdirs(directory, exist_ok = True)
?
exist ok = True
checks for existence and makes dir created if not existingOSErrors from the os package will do the error messaging ...
You're right @gwangjinkim. Just switch mkdirs to makedirs and it's good to go.
import os
os.makedirs(directory, exist_ok = True)
@EricHedengren thanks! True! I always have to look this up anew.
if i would like to create two folders in single directory how to do that
@khu17jain Write two makedirs statements to the same location. For loop it if you have a lot.
best code I have ever found, thx!
Thank you!
how to create a folder with name in format yyyy-mm-dd from any year to today
thank you man ``
Great! Thanks so much!
Thank you, very useful and helped me understand the command.
NIce! Thanks for posting.
Thks...