{"id":6141,"date":"2022-05-27T13:23:23","date_gmt":"2022-05-27T11:23:23","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=6141"},"modified":"2026-01-23T16:11:52","modified_gmt":"2026-01-23T15:11:52","slug":"the-os-module-in-python","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/05\/27\/the-os-module-in-python\/","title":{"rendered":"The os module in Python"},"content":{"rendered":"<p>A module called <i>os<\/i> lets you <b>interact with the operating system using Python<\/b>.\u00a0It provides functions that are available on Unix and\/or Windows systems. If you&#8217;re familiar with the command console, you&#8217;ll see that some functions give the same results as the commands available on the operating systems.<\/p>\n<p><!--more--><\/p>\n<p>1. The <strong><code>uname()<\/code> <\/strong>function returns an object that contains information about the current operating system. The object has the following attributes:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><i>systemname<\/i> (stores the name of the operating system)<\/li>\n<li><i>nodename<\/i> (stores the machine name on the network)<\/li>\n<li><i>release<\/i> (stores the operating system release)<\/li>\n<li><i>version<\/i> (stores the operating system version)<\/li>\n<li><i>machine<\/i> (stores the hardware identifier, e.g. x86_64.)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">import os\r\nprint(os.uname())<\/pre>\n<p>Unfortunately, the <code>uname() <\/code>function only works on some Unix systems. If you use Windows, you can use the <i>uname<\/i> function in the <i>platform<\/i> module, which returns a similar result.<\/p>\n<p>&nbsp;<\/p>\n<p>2. The <strong><code>name <\/code><\/strong>attribute available in the <code>os<\/code> module allows you to distinguish the operating system. It returns one of the following three values:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><i>posix<\/i> (you&#8217;ll get this name if you use Unix)<\/li>\n<li><i>nt<\/i> (you&#8217;ll get this name if you use Windows)<\/li>\n<li><i>java<\/i> (you&#8217;ll get this name if your code is written in something like Jython)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">import os\r\nprint(os.name)<\/pre>\n<p>&nbsp;<\/p>\n<p>3. The <code><strong>mkdir()<\/strong><\/code> function creates a directory in the path passed as its argument. The path can be either relative or absolute, e.g:<\/p>\n<pre class=\"lang:default decode:true\">import os\r\n\r\nos.mkdir(\"hello\") # the relative path\r\nos.mkdir(\"\/home\/python\/hello\") # the absolute path\r\n\r\n<\/pre>\n<p><b>Note<\/b>: If the directory exists, a <code>FileExistsError<\/code> exception will be thrown.<\/p>\n<p>To change the directory permissions, you can use the <code>chmod() <\/code>function, which works similarly to the <i>chmod<\/i> command on Unix systems.<\/p>\n<p>&nbsp;<\/p>\n<p>4. The <strong><code>makedirs()<\/code> <\/strong>function enables recursive directory creation, which means that all directories in the path will be created.<\/p>\n<pre class=\"lang:default decode:true \">import os\r\n\r\nos.makedirs(\"my_first_directory\/my_second_directory\")\r\nos.chdir(\"my_first_directory\")\r\nprint(os.listdir())<\/pre>\n<p><b>NOTE:<\/b> The equivalent of the <i>makedirs<\/i> function on Unix systems is the <i>mkdir<\/i> command with the <i>-p<\/i> flag, while in Windows, simply the <i>mkdir<\/i> command with the path:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Unix-like systems:\u00a0\u00a0<code>mkdir -p my_first_directory\/my_second_directory<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Windows: \u00a0<code>mkdir my_first_directory\/my_second_directory<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>5. The result of the <strong><code>listdir()<\/code><\/strong> function is a list containing the names of the files and directories that are in the path passed as its argument.<\/p>\n<p>It&#8217;s important to remember that the <code>listdir()<\/code> function omits the entries &#8216;.&#8217; and &#8216;..&#8217;, which are displayed, for example, when using the <code>ls -a<\/code> command on Unix systems. If the path isn&#8217;t passed, the result will be returned for the current working directory.<\/p>\n<pre class=\"lang:default decode:true\">import os\r\n\r\nos.mkdir(\"my_first_directory\")\r\nprint(os.listdir())\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>6.\u00a0To move between directories, you can use a function called <code><strong>chdir()<\/strong><\/code>, which changes the current working directory to the specified path. As its argument, it takes any relative or absolute path.<\/p>\n<p>&nbsp;<\/p>\n<p>7. If you want to find out what the current working directory is, you can use the <strong><code>getcwd()<\/code><\/strong> function, which returns the path to it.<\/p>\n<pre class=\"lang:default decode:true\">import os\r\n\r\nos.makedirs(\"my_first_directory\/my_second_directory\")\r\nos.chdir(\"my_first_directory\")\r\nprint(os.getcwd())\r\nos.chdir(\"my_second_directory\")\r\nprint(os.getcwd())\r\n<\/pre>\n<p><b>NOTE:<\/b> On Unix-like systems, the equivalent of the <i>getcwd<\/i> function is the <i>pwd<\/i> command, which prints the name of the current working directory.<\/p>\n<p>&nbsp;<\/p>\n<p>8. To remove a directory, you can use the <code><strong>rmdir()<\/strong><\/code> function.<\/p>\n<pre class=\"lang:default decode:true \">import os\r\n\r\nos.mkdir(\"my_first_directory\")\r\nprint(os.listdir())\r\nos.rmdir(\"my_first_directory\")\r\nprint(os.listdir())<\/pre>\n<p>To remove a directory and its subdirectories, use the <code><strong>removedirs()<\/strong><\/code> function.<\/p>\n<pre class=\"lang:default decode:true\">import os\r\n\r\nos.makedirs(\"my_first_directory\/my_second_directory\")\r\nos.removedirs(\"my_first_directory\/my_second_directory\")\r\nprint(os.listdir())\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>9. On both Unix and Windows, you can use the <code>system()<\/code> function, which executes a command passed to it as a string, e.g.:<\/p>\n<pre class=\"lang:default decode:true \">import os\r\n\r\nreturned_value = os.system(\"mkdir hello\")\r\n\r\n<\/pre>\n<p>The <code>system<\/code> function on Windows returns the value returned by shell after running the command given, while on Unix it returns the exit status of the process. Exit status <code>0<\/code> indicates success on Unix systems.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 1<\/em><\/p>\n<p>What is the output of the following snippet if you run it on Unix?<\/p>\n<pre class=\"lang:default decode:true \">import os \r\nprint(os.name)<\/pre>\n<p id=\"sol\"><code class=\"codep \">posix<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 2<\/em><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">import os\r\n\r\nos.mkdir(\"hello\")\r\nprint(os.listdir())<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol2\"><code class=\"codep \">['hello']<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 3<\/p>\n<p>What is a possible output from the code snippet below :<\/p>\n<pre class=\"lang:default decode:true \">from platform import machine\r\nprint(machine())<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Answer:<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>AMD64<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><em>Explanation:<\/em><\/p>\n<p>The <code>machine()<\/code> function from the <code>platform<\/code> module will return the generic name of the underlying processor, as a string.<\/p>\n<p>So depending on the machine you are using to run your Python code, this could return : x86, x86_64, AMD64, etc&#8230;<\/p>\n<p>The only answer that is a possible output from the <code>machine()<\/code> function is : <code>AMD64<\/code> .<\/p>\n<p>The other suggested answers are possible output from other functions from the <code>platform<\/code> module.<\/p>\n<p>Please refer to the <em>Try it yourself<\/em> section for more details.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"lang:default decode:true \"># results below will depend on your development environment (OS version, processor, etc...)\r\nfrom platform import platform, machine, processor, system, version, python_implementation\r\n \r\nprint(platform())\r\n# Windows-10-10.0.19041-SP0\r\n \r\nprint(machine())\r\n# AMD64\r\n \r\nprint(processor())\r\n# Intel64 Family 6 Model 78 Stepping 3, GenuineIntel\r\n \r\nprint(system())\r\n# Windows\r\n \r\nprint(version())\r\n# 10.0.19041\r\n \r\nprint(python_implementation())\r\n# CPython<\/pre>\n<p>&nbsp;<\/p>\n<p>Ex.4<\/p>\n<div id=\"question-prompt\" class=\" ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>You want to check which version of Python your development environment is using.\u00a0Which code snippet below would provide this information ?<\/p>\n<p>A.<\/p>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">import platform\r\nprint(platform.python_implementation())<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p>B.<\/p>\n<pre class=\"lang:default decode:true \">import platform\r\nprint(platform.platform())<\/pre>\n<p>C.<\/p>\n<pre class=\"lang:default decode:true \">import platform\r\nprint(platform.python_version_tuple())\r\n<\/pre>\n<p>D.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">import platform\r\nprint(platform.version())<\/pre>\n<\/div>\n<p>Explanation:<\/p>\n<p>The <code>platform<\/code> module contains functions which provides information on the Python environment, the underlaying Operating System and the hardware.<\/p>\n<p>The <code>platform()<\/code>\u00a0 function returns a string describing the environment (OS + processor).<\/p>\n<p>The <code>version()<\/code>\u00a0 function returns the OS version as a string.<\/p>\n<p>The <code>python_implementation()<\/code> function returns the Python implementation as a string (most common is &#8220;CPython&#8221;).<\/p>\n<p>The <code>python_version_tuple()<\/code>\u00a0 returns a three-element tuple filled with the Python version (major part, minor part, patch level number).<\/p>\n<p>So, the correct answer is :<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">import platform\r\nprint(platform.python_version_tuple())<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A module called os lets you interact with the operating system using Python.\u00a0It provides functions that are available on Unix and\/or Windows systems. If you&#8217;re familiar with the command console, you&#8217;ll see that some functions give the same results as the commands available on the operating systems.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/6141"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=6141"}],"version-history":[{"count":13,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/6141\/revisions"}],"predecessor-version":[{"id":6279,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/6141\/revisions\/6279"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=6141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=6141"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=6141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}