From 5cdf9fd0ea27a169145df269c2d460ff52e452eb Mon Sep 17 00:00:00 2001 From: rasbt Date: Tue, 19 Nov 2013 23:06:44 -0500 Subject: [PATCH] fileops --- os_shutil_fileops | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 os_shutil_fileops diff --git a/os_shutil_fileops b/os_shutil_fileops new file mode 100644 index 0000000..d517f22 --- /dev/null +++ b/os_shutil_fileops @@ -0,0 +1,22 @@ +# sr 11/19/2013 +# common file operations in os and shutil modules + +import shutil +import os + +# Getting files of particular type from directory +files = [f for f in os.listdir(s_pdb_dir) if f.endswith(".txt")] + +# Copy and move +shutil.copyfile("/path/to/file", "/path/to/new/file") +shutil.copy("/path/to/file", "/path/to/directory") +shutil.move("/path/to/file","/path/to/directory") + +# Check if file or directory exists +os.path.exists("file or directory") +os.path.isfile("file") +os.path.isdir("directory") + +# Working directory and absolute path to files +os.getcwd() +os.path.abspath("file")