0

I want to get the name of a file that gets created by some other process inside a folder. It's in a path where only this file exists. It's in this path:

/directory/which/only/contains/this/one/file/

The file's full filename could then be for example:

/directory/which/only/contains/this/one/file/xZqsdtae123456.txt

How can I get the name of this file (from the example above: xZqsdtae123456.txt) and then save it in a variable ?

0

1 Answer 1

3

Assuming the generated file is the only one (as stated):

import os
yourfile = os.listdir("/home/user/Desktop/directory_which_only_contains_this_one_file")[0]
Sign up to request clarification or add additional context in comments.

1 Comment

and how would one loop over the files under this directory assuming we dont have only one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.