0

I write small scripts as educational projects. In the code below, we get the list of files as List[Union[Path, str]]. For the further operation of the program, nothing else is necessary.

There is a script and a separate test folder with all the files for the test. We open the directory where we ran the script and get all the files from it.

I am using the following code to find and open files:

from pathlib import Path
from typing import Iterator, List, Union


def merge_sorted_files(file_list: List[Union[Path, str]]) -> Iterator:
    m = []
    for filename in file_list:
        path_to_file = Path(Path.cwd(), filename)
        with open(path_to_file) as fi:
            for i in fi:
                m.append(int(i))
    return iter(sorted(m))

Everything works fine if run locally at the Linux platform, but GitHub actions fail to test. It uses the following environment: platform Linux -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 I get an error below (the path that my script collects does not exist)

=================================== FAILURES ===================================
__________________________________ test_merge __________________________________

    def test_merge():
>       assert isinstance(merge_sorted_files(['file1.txt', 'file2.txt']), Iterable)

tests/homework9/test_task1.py:7: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

file_list = ['file1.txt', 'file2.txt']

    def merge_sorted_files(file_list: List[Union[Path, str]]) -> Iterator:
        m = []
        for filename in file_list:
            path_to_file = Path(Path.cwd(), filename)
>           with open(path_to_file) as fi:
E           FileNotFoundError: [Errno 2] No such file or directory: '/__w/epamhw/epamhw/file1.txt'

homework9/hw1.py:9: FileNotFoundError

I found that the problem could be in the call to Path.cwd()because it returns the directory in which we are running the script and not the directory in which the file with the code is located.

I tried to replace it with os.path.dirname(os.path.realpath(__file__).

Code changes for this situation: path_to_file = Path(os.path.dirname(os.path.realpath(__file__)), filename) But the changes lead to the same failure.

=================================== FAILURES ===================================
__________________________________ test_merge __________________________________

    def test_merge():
>       assert isinstance(merge_sorted_files(['file1.txt', 'file2.txt']), Iterable)

tests/homework9/test_task1.py:7: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

file_list = ['file1.txt', 'file2.txt']

    def merge_sorted_files(file_list: List[Union[Path, str]]) -> Iterator:
        m = []
        for filename in file_list:
            path_to_file = Path(os.path.
                                dirname(os.path.realpath(__file__)), filename)
>           with open(path_to_file) as fi:
E           FileNotFoundError: [Errno 2] No such file or directory: '/__w/epamhw/epamhw/homework9/file1.txt'

homework9/hw1.py:11: FileNotFoundError

I only solved the problem when directly passing the directory from the test. A long search didn't help me concerning the issue described above.

The following code works for me, but it requires passing a directory to the parameters for listing files. But, according to the condition of the task, the script should not accept anything other than List[Union[Path, str]].

from pathlib import Path
from typing import Iterator, List, Union


def merge_sorted_files(dir_name,
                       file_list: List[Union[Path, str]]) -> Iterator:
    m = []
    for filename in file_list:
        file_path = Path(dir_name, filename)
        with open(file_path) as fi:
            for i in fi:
                m.append(int(i))
    return iter(sorted(m))


if __name__ == '__main__':
    merge_sorted_files()

Is there a way to find a directory with test files, and not pass it in the function arguments?

kuled
  • 1
  • Does this answer your question? [How do I get the full path of the current file's directory?](https://stackoverflow.com/questions/3430372/how-do-i-get-the-full-path-of-the-current-files-directory) – JonSG Dec 22 '21 at 18:15

0 Answers0