Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support *paths in touch() #127

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions audeer/core/io.py
audeerington marked this conversation as resolved.
Show resolved Hide resolved
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