Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allowdelete=False for tempfile.TemporaryDirectory() #100131

Closed
Assignees
gpshead
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
@JakobDev

Description

@JakobDev

Feature or enhancement

If you usetempfile.TemporaryDirectory(), the directory is automagically deleted, what is the whole purpose of this class. But sometimes you may want to take a look at it for debugging reasons, so it would be nice to have adelete=False to prevent deleting.

Pitch

Code using this could look like this:

importargparseimporttempfiledefmain():parser=argparse.ArgumentParser()parser.add_argument("--keep-temp-files",action="store_true",help="Don't delete temporary files")args=parser.parse_args()withtempfile.TemporaryDirectory(delete=notargs.keep_temp_files)astempdir:# Do somethingifargs.keep_temp_files:print(f"You can view your temporary files at{tempdir}")if__name__=="__main__":main()

I personal usetempfile.TemporaryDirectory() a lot, especial in build scripts. Sometimes a need to take a look at the content of these. so having this Option would be great for that.

Other ways to achieve this

Of course, there would be other ways to achieve this.

1. Use a extra function that takes the directory as argument:

ifargs.keep_temp_files:do_something("some/other/dir")else:withtempfile.TemporaryDirectory()astempdir:do_something(tempfile)

This would need some code rewrite.

2. Usinginput()

withtempfile.TemporaryDirectory()astempdir:# Do somethingifargs.keep_temp_files:print(f"You can view your temporary files at{tempdir}. Press enter to delete the files.")breakpoint()

This could cause problems in situations, where you can't easily give some Keyboard input to TTY.

3. Using a custom function

defmy_temp_dir(delet:bool=false):ifdelete:returntempfile.TemporaryDirectory()else:returnmy_custom_handler()

This will blow up your code when you just write a simple script. Python also often used by beginners, which may don't know, they can do this and start refactoring their code.

Why you should implement this

The argument can be easy added to exiting scripts if needed, without any additional work. A scripting language like Python should allow this to make debug more easy and more beginner friendly.

It is absolutely no work to implement this. If this would be much work to implement and maintain this, I would not suggested it. But in this case, it's just 3 steeps:

  1. Take a additional argument in__init__():
  2. Save is as class attribute
  3. In__exit__() ask, if it should delete or not

Previous discussion

None

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp