PermissionError: [Errno 13] Permission denied: output.csv

Viewed 2145

After the user provides the source directory, the following script reads in a list of csvs. It then takes one csv and copies its contents row by row to a new csv until it reaches 100,000 rows at which point a new csv is created to continue the process until the original csv has been copied completely. The process is then repeated for the next csv file in the directory.

I will sometimes encounter the above PermissionError and am not sure how to go about fixing it, but sometimes I will run the script and I encounter no issues. I've verified that both the input and output files are NOT open on my machine. I've also tried to change the properties of my directory folder to not be read-only, though this always reverts back. When the error does occur, it is always within a few seconds of first starting to process a csv. Once you are about 5 seconds in, it won't give the error for that csv. But it could later once it gets to a new input csv.

"""
    Script processes all csv's in a provided directory and returns
    csv's with a maximum of 100,000 rows
"""

import csv
import pathlib
import argparse
import os
import glob


def _get_csv_list(
        *, description: str = "Process csv file directory.",
):
    """
        Uses argument parser to set up working directory, then
        extracts list of csv file names from directory

        Args: Directory string

        Returns list of csv file name strings
    """

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument(
        "SRC", type=pathlib.Path, help="source (input) directory"
    )
    parsed_arg = parser.parse_args()
    os.chdir(parsed_arg.SRC)
    return glob.glob("*.{}".format("csv"))


def _process_csv(file_name):
    """
        Iterates through csv file and copies each row to output
        file.  Once 100,000 rows is reached, a new file is started

        Args: file name string

    """
    file_index = 0
    max_records_per_file = 100_000
    with open(file_name) as _file:
        reader = csv.reader(_file)
        first_line = _file.readline()
        first_line_list = first_line.split(",")

        for index, row in enumerate(reader):

            if index % max_records_per_file == 0:
                file_index += 1
                with open(
                        f"output_{file_name.strip('.csv')}_{file_index}.csv",
                        mode="xt",
                        encoding="utf-8",
                        newline="\n",
                ) as buffer:
                    writer = csv.writer(buffer)
                    writer.writerow(first_line_list)

            else:
                try:
                    with open(
                            f"output_{file_name.strip('.csv')}_{file_index}.csv",
                            mode="at",
                            encoding="utf-8",
                            newline="\n",
                    ) as buffer:
                        writer = csv.writer(buffer)
                        writer.writerow(row)
                except FileNotFoundError as error:
                    print(error)
                    with open(
                            f"output_{file_name.strip('.csv')}_{file_index}.csv",
                            mode="xt",
                            encoding="utf-8",
                            newline="\n",
                    ) as buffer:
                        writer = csv.writer(buffer)
                        writer.writerow(first_line_list)
                        writer.writerow(row)


def main():
    """
        Primary function for limiting csv file size

        Cmd Line: python csv_row_limiter.py .   (Replace '.' with other path
        if csv_row_limiter.py directory and csv directory are different)
    """
    csv_list = _get_csv_list()
    for file_name in csv_list:
        _process_csv(file_name)


if __name__ == "__main__":
    main()

Also, please note that the only requirement for the contents of the input csv's is that they have a large number of rows (100,000+) with some amount of data.

Any ideas of how I might resolve this issue?

1 Answers

try opening it as root i.e try running this python script through root or su privileges. What i mean is login as root and then run this python script . Hope this helps.

Related