Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes in section "Python OS" #124

Merged
merged 5 commits into from
Jan 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6297,18 +6297,42 @@ with open('file.txt', 'w') as file:

<details>
<summary>How to print current working directory?</summary><br><b>

import os

print(os.getcwd())

</b></details>

<details>
<summary>Given the path <code>/dir1/dir2/file1</code> print the file name (file1)</summary><br><b>
</b></details>

<details>
<summary>Given the path <code>/dir1/dir2/file1</code> print the name of the directory where the file resides (dir2)</summary><br><b>
import os

print(os.path.basename('/dir1/dir2/file1'))

# Another way
print(os.path.split('/dir1/dir2/file1')[1])

</b></details>

<details>
<summary>Given the path <code>/dir1/dir2/file1</code> print the path without the file name (/dir1/dir2)</summary><br><b>
<summary>Given the path <code>/dir1/dir2/file1</code>

1. Print the path without the file name (/dir1/dir2)
2. Print the name of the directory where the file resides (dir2)
</summary><br><b>

import os

## Part 1.
# os.path.dirname gives path removing the end component
dirpath = os.path.dirname('/dir1/dir2/file1')
print(dirpath)

## Part 2.
print(os.path.basename(dirpath))

</b></details>

<details>
Expand Down