{"id":4756,"date":"2013-10-08T05:40:32","date_gmt":"2013-10-08T05:40:32","guid":{"rendered":"https:\/\/www.pythonforbeginners.com\/?p=4756"},"modified":"2020-08-25T19:27:54","modified_gmt":"2020-08-26T00:27:54","slug":"python-system-administration","status":"publish","type":"post","link":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration","title":{"rendered":"Python System Administration"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>The OS module in Python provides a way of using operating system dependent<br \/>\nfunctionality.<\/p>\n<p>The functions that the OS module provides allows you to interface with the<br \/>\nunderlying operating system that Python is running on. (Windows, Mac or<br \/>\nLinux.<\/p>\n<p>You can find important information about your location or about the process.<\/p>\n<p>Before we start, make sure that you have imported the OS module &#8220;<em>import os<\/em>&#8221;<\/p>\n<h2>OS Functions explained<\/h2>\n<pre><code class=\"language-python\">os.system()\t# Executing a shell command\n\nos.stat()\t# Get the status of a file\n\nos.environ()    # Get the users environment\n\nos.chdir()   \t# Move focus to a different directory\n\nos.getcwd()    \t# Returns the current working directory\n\nos.getgid()    \t# Return the real group id of the current process\n\nos.getuid()    \t# Return the current process\u2019s user id\n\nos.getpid()     # Returns the real process ID of the current process\n\nos.getlogin()   # Return the name of the user logged\n\nos.access()   \t# Check read permissions\n\nos.chmod()    \t# Change the mode of path to the numeric mode\n\nos.chown()   \t# Change the owner and group id\n\nos.umask(mask)  # Set the current numeric umask\n\nos.getsize()   \t# Get the size of a file\n\nos.environ()    # Get the users environment\n\nos.uname()   \t# Return information about the current operating system\n\nos.chroot(path) # Change the root directory of the current process to path\n\nos.listdir(path)# List of the entries in the directory given by path\n\nos.getloadavg() # Show queue averaged over the last 1, 5, and 15 minutes\n\nos.path.exists()# Check if a path exists\n\nos.walk()   \t# Print out all directories, sub-directories and files\n\nos.mkdir(path)\t# Create a directory named path with numeric mode mode\n\nos.remove(path)\t# Remove (delete) the file path\n\nos.rmdir(path)  # Remove (delete) the directory path\n\nos.makedirs(path)# Recursive directory creation function\n\nos.removedirs(path) # Remove directories recursively\n\nos.rename(src, dst) # Rename the file or directory src to dst<\/code><\/pre>\n<h2>OS Functions Examples<\/h2>\n<p>Let&#8217;s get started to see how we can use these OS functions.<\/p>\n<pre><code class=\"language-python\">#Get current working directory with os.getcwd()\nprint os.getcwd()\n\n#Get the status of a file with os.stat()\nprint \"Getting the status of: \", os.stat('\/usr\/bin\/python')\n\n#Execute a shell command with os.system()\nos.system('ls -l')\n\n#Return the current process id with os.getpid()\nprint os.getpid()\nos.chmod(path, mode)\n\n#Change the owner and group id of path to the numeric uid and gid with os.chown()\nos.chown(path, uid, gid)\n\n#Processes in the system run queue averaged over the last 1, 5, and 15 minutes\nprint os.getloadavg()\n\n#Check if a path exists with os.path.exists()\nif os.path.exists(\"file.txt\"):\n\n#Create a new directory named 'new_directory' if it doesn't exist already\"\nos.path.exists(\"new_directory\") or os.mkdir(\"new_directory\")\n\n#Check if the path is a directory or a file with os.path.isdir() &amp; os.path.isfile()\npath = \"\/tmp\"\nif os.path.isdir(path): print \"That's a directory\"\nif os.path.isfile(path): print \"That's a file\"\n\n#Create a directory with os.makedir()\nprint os.mkdir('new_directory', 0666)\n\n#Recursive create directories with os.makedirs()\nos.makedirs('dir_a\/dir_b\/dir_c')\n\n#Remove a directory with os.rmdir()\nprint os.rmdir('directory')\n\n#Recursively remove empty directories with os.rmdirs()\nos.removedirs('dir_a\/dir_b\/dir_c')\n\n#Rename a file with os.rename()\nprint os.rename('\/path\/to\/old\/file', '\/path\/to\/new\/file')\n\n#Rename a file with shutil.move()\nprint shutil.move('\/path\/to\/old\/file', '\/path\/to\/new\/file')\n\n#Rename a file with shutil.copy()\nprint shutil.copy('\/path\/to\/old\/file', '\/path\/to\/new\/file')\n\n#Get the users home directory\nprint os.path.expanduser('~')\n\n#Check read permissions with os.access()\npath = '\/tmp\/file.txt'\nprint os.access(path, os.R_OK)\n\n#Get the users environment with os.environmen()\nhome =  os.environ['HOME']\nprint home\n\n#Move focus to a different directory with os.chdir()\nprint os.chdir('\/tmp')\n\n#Print out all directories, sub-directories and files with os.walk()\nfor root, dirs, files in os.walk(\"\/tmp\"):\n    print root\n    print dirs\n    print files\n\n#Get the last time a directory was accessed with os.path.getatime()\nos.path.getatime('\/tmp')\n\n#Get the last time a directory was modified with os.path.getmtime()\nos.path.getmtime('\/tmp')\n\n#Get the user ID with os.getuid()\nif os.getuid() != 0: print \"you are not root\"\n\n#Get the group ID with os.getgid()\nprint os.getgid()\n\n#Return the name of the user logged in with os.getlogin()\nprint os.getlogin()\n\n#Returns a list of all files in a directory with os.listdir()\nfor filename in os.listdir(\"\/tmp\"):\n    print \"This is inside \/tmp\", filename\n\n#Get the size of a file with os.path.getsize()\npath.getsize(\"\/tmp\/file.txt\")<\/code><\/pre>\n<p>Using Python in your daily work is a good way to automate system administration tasks, when you feel that your shell scripts are to limited.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview The OS module in Python provides a way of using operating system dependent functionality. The functions that the OS module provides allows you to interface with the underlying operating system that Python is running on. (Windows, Mac or Linux. You can find important information about your location or about the process. Before we start, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[38,97],"tags":[],"class_list":{"0":"post-4756","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-os","7":"category-systems-programming","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python System Administration - PythonForBeginners.com<\/title>\n<meta name=\"description\" content=\"Python System Administration will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python System Administration - PythonForBeginners.com\" \/>\n<meta property=\"og:description\" content=\"Python System Administration will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration\" \/>\n<meta property=\"og:site_name\" content=\"PythonForBeginners.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pythonbeginners\" \/>\n<meta property=\"article:published_time\" content=\"2013-10-08T05:40:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-26T00:27:54+00:00\" \/>\n<meta name=\"author\" content=\"PFB Staff Writer\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:site\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PFB Staff Writer\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration\"},\"author\":{\"name\":\"PFB Staff Writer\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\"},\"headline\":\"Python System Administration\",\"datePublished\":\"2013-10-08T05:40:32+00:00\",\"dateModified\":\"2020-08-26T00:27:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration\"},\"wordCount\":113,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"articleSection\":[\"OS\",\"System &amp; OS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration\",\"name\":\"Python System Administration - PythonForBeginners.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\"},\"datePublished\":\"2013-10-08T05:40:32+00:00\",\"dateModified\":\"2020-08-26T00:27:54+00:00\",\"description\":\"Python System Administration will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/systems-programming\\\/python-system-administration#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python System Administration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"name\":\"PythonForBeginners.com\",\"description\":\"Learn By Example\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\",\"name\":\"PythonForBeginners.com\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"width\":1868,\"height\":318,\"caption\":\"PythonForBeginners.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/pythonbeginners\",\"https:\\\/\\\/x.com\\\/pythonbeginners\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\",\"name\":\"PFB Staff Writer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"caption\":\"PFB Staff Writer\"},\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/author\\\/pfb_staff\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python System Administration - PythonForBeginners.com","description":"Python System Administration will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration","og_locale":"en_US","og_type":"article","og_title":"Python System Administration - PythonForBeginners.com","og_description":"Python System Administration will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","og_url":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration","og_site_name":"PythonForBeginners.com","article_publisher":"https:\/\/www.facebook.com\/pythonbeginners","article_published_time":"2013-10-08T05:40:32+00:00","article_modified_time":"2020-08-26T00:27:54+00:00","author":"PFB Staff Writer","twitter_card":"summary_large_image","twitter_creator":"@pythonbeginners","twitter_site":"@pythonbeginners","twitter_misc":{"Written by":"PFB Staff Writer","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration#article","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration"},"author":{"name":"PFB Staff Writer","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648"},"headline":"Python System Administration","datePublished":"2013-10-08T05:40:32+00:00","dateModified":"2020-08-26T00:27:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration"},"wordCount":113,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"articleSection":["OS","System &amp; OS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration","url":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration","name":"Python System Administration - PythonForBeginners.com","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/#website"},"datePublished":"2013-10-08T05:40:32+00:00","dateModified":"2020-08-26T00:27:54+00:00","description":"Python System Administration will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","breadcrumb":{"@id":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonforbeginners.com\/systems-programming\/python-system-administration#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonforbeginners.com\/"},{"@type":"ListItem","position":2,"name":"Python System Administration"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonforbeginners.com\/#website","url":"https:\/\/www.pythonforbeginners.com\/","name":"PythonForBeginners.com","description":"Learn By Example","publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonforbeginners.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonforbeginners.com\/#organization","name":"PythonForBeginners.com","url":"https:\/\/www.pythonforbeginners.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","contentUrl":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","width":1868,"height":318,"caption":"PythonForBeginners.com"},"image":{"@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/pythonbeginners","https:\/\/x.com\/pythonbeginners"]},{"@type":"Person","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648","name":"PFB Staff Writer","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","caption":"PFB Staff Writer"},"url":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"}]}},"jetpack_publicize_connections":[],"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"PFB Staff Writer","author_link":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"},"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/4756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/comments?post=4756"}],"version-history":[{"count":4,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/4756\/revisions"}],"predecessor-version":[{"id":7428,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/4756\/revisions\/7428"}],"wp:attachment":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/media?parent=4756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/categories?post=4756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/tags?post=4756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}