Skip to content

Commit

Permalink
Support *paths in touch() (#127)
Browse files Browse the repository at this point in the history
* Support *paths in touch()

* Update examples of list_file_names()

* Update example of move_file()
  • Loading branch information
hagenw authored Dec 5, 2023
1 parent 48fdf9f commit ef3b271
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
20 changes: 12 additions & 8 deletions audeer/core/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,12 @@ def list_file_names(
Examples:
>>> dir_path = mkdir('path')
>>> _ = touch(os.path.join(dir_path, 'file.wav'))
>>> _ = touch(os.path.join(dir_path, 'File.wav'))
>>> _ = touch(os.path.join(dir_path, '.lock'))
>>> _ = touch(dir_path, 'file.wav')
>>> _ = touch(dir_path, 'File.wav')
>>> _ = touch(dir_path, '.lock')
>>> sub_dir_path = mkdir('path', 'sub')
>>> _ = touch(os.path.join(sub_dir_path, 'file.ogg'))
>>> _ = touch(os.path.join(sub_dir_path, '.lock'))
>>> _ = touch(sub_dir_path, 'file.ogg')
>>> _ = touch(sub_dir_path, '.lock')
>>> list_file_names(
... dir_path,
... basenames=True,
Expand Down Expand Up @@ -860,7 +860,7 @@ def move_file(
Examples:
>>> path = mkdir('folder')
>>> src_path = touch(os.path.join(path, 'file1'))
>>> src_path = touch(path, 'file1')
>>> dst_path = os.path.join(path, 'file2')
>>> move_file(src_path, dst_path)
>>> list_file_names(path, basenames=True)
Expand Down Expand Up @@ -969,7 +969,8 @@ def rmdir(


def touch(
path: typing.Union[str, bytes]
path: typing.Union[str, bytes],
*paths: typing.Sequence[typing.Union[str, bytes]],
) -> str:
"""Create an empty file.
Expand All @@ -979,6 +980,9 @@ def touch(
Args:
path: path to file
*paths: additional arguments
to be joined with ``path``
by :func:`os.path.join`
Returns:
expanded path to file
Expand All @@ -989,7 +993,7 @@ def touch(
'file.txt'
"""
path = safe_path(path)
path = safe_path(path, *paths)
if os.path.exists(path):
os.utime(path, None)
else:
Expand Down
7 changes: 3 additions & 4 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def test_list_dir_names(tmpdir, dir_list, expected, recursive, hidden):

def test_list_dir_names_errors(tmpdir):
with pytest.raises(NotADirectoryError):
file = audeer.touch(audeer.path(tmpdir, 'file.txt'))
file = audeer.touch(tmpdir, 'file.txt')
audeer.list_dir_names(file)
with pytest.raises(FileNotFoundError):
audeer.list_dir_names('not-existent')
Expand Down Expand Up @@ -1153,7 +1153,7 @@ def test_move_file(tmpdir, src_file, dst_file):
tmp_path = str(tmpdir.mkdir('folder'))
tmp_path = audeer.mkdir(tmp_path)

src_path = audeer.touch(os.path.join(tmp_path, src_file))
src_path = audeer.touch(tmp_path, src_file)
dst_path = os.path.join(tmp_path, dst_file)

audeer.move_file(src_path, dst_path)
Expand Down Expand Up @@ -1220,8 +1220,7 @@ def test_rmdir(tmpdir):


def test_touch(tmpdir):
path = str(tmpdir.mkdir('folder1'))
path = audeer.mkdir(path)
path = audeer.mkdir(tmpdir, 'folder1')
path = os.path.join(path, 'file')
assert not os.path.exists(path)
audeer.touch(path)
Expand Down

0 comments on commit ef3b271

Please sign in to comment.