Last active
March 24, 2016 22:26
-
-
Save michaelconnor00/dd4c0e18f5d1fd501052 to your computer and use it in GitHub Desktop.
File Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-in-python | |
# Using glob | |
import glob, os | |
os.chdir("/mydir") | |
for file in glob.glob("*.txt"): | |
print(file) | |
# Using listdir | |
import os | |
for file in os.listdir("/mydir"): | |
if file.endswith(".txt"): | |
print(file) | |
# Using os.walk | |
import os | |
for root, dirs, files in os.walk("/mydir"): | |
for file in files: | |
if file.endswith(".txt"): | |
print(os.path.join(root, file)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get list of all files current working directory | |
import os | |
for dirpath, dirs, files in os.walk(os.getcwd()): | |
# Do stuff | |
for f in files: | |
os.rename(f, 'hi_%s' % f) | |
# Get path to file relative to the execution of the script. | |
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)) as f: | |
workflow_json = f.read() | |
Great_source = 'https://pymotw.com/2/ospath/' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment