Is it possible to get the path of a tempfile in Python 3

前端 未结 3 906
[愿得一人]
[愿得一人] 2021-01-17 10:34

I was wondering if it was possible to get the file path of a temporary file made using the tempfile library. Basically, I\'m trying to make a function that intakes some data

3条回答
  •  旧时难觅i
    2021-01-17 11:02

    tempfile.NamedTemporaryFile has a .dir property which will give you want you want.


    EDIT: No, it is not .name, @Barmar, but looking through the source code for tempfile, I don't see a .dir property either. However, you can use the .name property in conjunction with os.path's dirname method as follows:

    with tempfile.NamedTemporaryFile(suffix='.csv', prefix=os.path.basename(__file__)) as tf:
        tf_directory = os.path.dirname(tf.name)
    

提交回复
热议问题