Skip to content

Instantly share code, notes, and snippets.

@azm-gh
Created January 27, 2019 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azm-gh/3fadc4bd1fef28bb9d649cb6adae4f30 to your computer and use it in GitHub Desktop.
Save azm-gh/3fadc4bd1fef28bb9d649cb6adae4f30 to your computer and use it in GitHub Desktop.
working-with-files-in-python
import os
from pathlib import Path
with open('data.txt', 'w') as f:
data = 'some data to be written in a file'
f.write(data)
with open('data.txt', 'r') as f:
data = f.read()
entries = os.listdir('C:\\Users\\Jimmy\\PycharmProjects\\untitled')
print(entries)
entries = os.listdir('C:\\Users\\Jimmy\\PycharmProjects\\untitled')
for entry in entries:
print(entry)
with os.scandir('C:\\Users\\Jimmy\\PycharmProjects\\untitled') as entrries2:
for entry in entrries2:
print(entry.name)
entries3 = Path('C:\\Users\\Jimmy\\PycharmProjects\\untitled')
for entry in entries3.iterdir():
print(entry.name)
'''Using pathlib.Path() or os.scandir() instead of os.listdir()
is the preferred way of getting a directory listing, especially when you’re working
with code that needs the file type and file attribute information.
pathlib.Path() offers much of the file and path handling functionality
found in os and shutil, and it’s methods are more efficient than some found in these modules.
We will discuss how to get file properties shortly.'''
print('#--------List all files in a director using os.listdir----------#')
#List all files in a director using os.listdir
basepath = 'C:\\Users\\Jimmy\\PycharmProjects\\untitled'
for entry in os.listdir(basepath):
if os.path.isfile(os.path.join(basepath, entry)):
print(entry)
print('#----------List all files in a directory using scandir-----------#')
#List all files in a directory using scandir
basepath = 'C:\\Users\\Jimmy\\PycharmProjects\\untitled'
with os.scandir(basepath) as entries:
for entry in entries:
if entry.is_file():
print(entry.name)
print('#----------List all files in a directory using pathlib-----------#')
basepath = Path('C:\\Users\\Jimmy\\PycharmProjects\\untitled')
files_in_basepath = basepath.iterdir()
for item in files_in_basepath:
if item.is_file():
print(item.name)
print('#----------List all files in a directory using pathlib using generators-----------#')
basepath = Path('C:\\Users\\Jimmy\\PycharmProjects\\untitled')
files_in_basepath = (entry for entry in basepath.iterdir() if entry.is_file())
for item in files_in_basepath:
print(item.name)
print('#----------List all subdirectories using os.listdir-----------#')
basepath = 'C:\\Users\\Jimmy\\PycharmProjects'
for entry in os.listdir(basepath):
if os.path.isdir(os.path.join(basepath, entry)):
print(entry)
print('#----------List all subdirectories using os.scandir----------#')
basepath = 'C:\\Users\\Jimmy\\PycharmProjects'
with os.scandir(basepath) as entries:
for entry in entries:
if entry.is_dir():
print(entry.name)
print('#----------List all subdirectories using basepath---------#')
basepath = 'C:\\Users\\Jimmy\\PycharmProjects'
for entry in basepath.iterdir():
if entry.is_dir():
print(entry.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment