Tag Archives: FIles

Deleting files and folders in Python

Deleting files

Use os.remove()

import os
os.remove(filename)

Deleting folders

Use os.rmdir()

import os
os.rmdir(dirname)

You will get an OSError if the directory is not empty.

Deleting whole trees

You can delete a non-empty directory and all of its descendants with shutil.rmtree()

shutil.rmtree(dirname)

Listing all files in a directory with a certain extension in Python

We want to be able to do this:

def main():
    directory = '/home/martin/Python'
    files = list_files(directory, "py")
    for f in files:
        print f
spam.py
eggs.py
ham.py

Method1: Use os.listdir()

from os import listdir

def list_files1(directory, extension):
    return (f for f in listdir(directory) if f.endswith('.' + extension))

Method 2: Use os.walk()

Note that walk() will recurse into subdirectories, but we avoid this by returning on the first iteration of the loop.

from os import walk

def list_files2(directory, extension):
    for (dirpath, dirnames, filenames) in walk(directory):
        return (f for f in filenames if f.endswith('.' + extension))

Method 3: Use glob.glob()

You need to change into the directory to use glob(), so it’s good manners to save the current working directory and then change back into it at the end.

from glob import glob
from os import getcwd, chdir

ef list_files3(directory, extension):
    saved = getcwd()
    chdir(directory)
    it = glob('*.' + extension)
    chdir(saved)
    return it

List the Files in a Directory in C++

Imagine that we want to populate a vector with the names of the files in a directory with a read_directory() function like this:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

typedef std::vector<std::string> stringvec;

int main()
{
    stringvec v;
    read_directory(".", v);
    std::copy(v.begin(), v.end(),
         std::ostream_iterator<std::string>(std::cout, "\n"));
}

Method 1: Use boost_filesystem

#include <boost/filesystem.hpp>

struct path_leaf_string
{
    std::string operator()(const boost::filesystem::directory_entry& entry) const
    {
        return entry.path().leaf().string();
    }
};

void read_directory(const std::string& name, stringvec& v)
{
    boost::filesystem::path p(name);
    boost::filesystem::directory_iterator start(p);
    boost::filesystem::directory_iterator end;
    std::transform(start, end, std::back_inserter(v), path_leaf_string());
}

Reference: Boost Filesystem Library Version 3

Method 2: Use opendir()/readdir()/closedir() (POSIX)

#include <sys/types.h>
#include <dirent.h>

void read_directory(const std::string& name, stringvec& v)
{
    DIR* dirp = opendir(name.c_str());
    struct dirent * dp;
    while ((dp = readdir(dirp)) != NULL) {
        v.push_back(dp->d_name);
    }
    closedir(dirp);
}

Reference: readdir – The Open Group

Method 3: Use FindFirstFile()/FindNextFile()/FindClose() (Windows)

#include <windows.h>

void read_directory(const std::string& name, stringvec& v)
{
    std::string pattern(name);
    pattern.append("\\*");
    WIN32_FIND_DATA data;
    HANDLE hFind;
    if ((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
        do {
            v.push_back(data.cFileName);
        } while (FindNextFile(hFind, &data) != 0);
        FindClose(hFind);
    }
}

Reference: FindFirstFile function

Checking whether a file exists in Python

You can use os.path.isfile() to check both whether a name both exists and is a file:

from os.path import isfile

if isfile('file.txt'):
    # Do something with file.txt

To check for the existence of any type of filesystem object, use os.path.exists():

from os.path import exists

if exists('path'):
    # Do something with path

Note that it isn’t a good idea to check for a file’s existence before opening it – it’s a race condition, with the file able to disappear in-between you testing for its existence and opening it.

It’s much better (and more Pythonic) to just try to open it and handle failure:

try:
    with open('file.txt') as f:
        # Do something with f
except IOError as err:
    # Handle the err

Read a file into a string in C++

Method

  1. Open the file
  2. Seek to the end and use tell to find out how big it is
  3. Seek back to the beginning
  4. Construct a string with the length + 1 and fill it with nul characters so it’s nul-terminated
  5. Read the contents into the string’s buffer

You can do this using iostreams or stdio.

Using iostreams

    std::ifstream in(filename);
    if (in) {
        in.seekg(0, std::ios::end);
        size_t len = in.tellg();
        in.seekg(0);
        std::string contents(len + 1, '\0');
        in.read(&contents[0], len);
    }

Using stdio

    std::FILE* fptr = std::fopen(filename.c_str(), "r");
    if (fptr) {
        std::fseek(fptr, 0, SEEK_END);
        size_t len = std::ftell(fptr);
        std::fseek(fptr, 0, SEEK_SET);
        std::string contents(len + 1, '\0');
        std::fread(&contents[0], 1, len, fptr);
        fclose(fptr);
    }

Listing all files in a directory in Python

There are 2 ways of doing this: using os.listdir(), or using os.walk().

Use os.listdir()

os.listdir() will give you files and directories. You can filter them with a list comprehension as follows:

import os
import os.path

files = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]

Use os.walk()

os.walk() separates files and directories, and will recurse into directories. To get only the files in the current directory, just save the third element of the tuple and break out of the loop:

import os

files = []
for (dirpath, dirnames, filenames) in os.walk(mypath):
    files.extend(filenames)
    break