Ruby Temporary Files in a Temporary Directory

I want to create temporary files in a temporary directory. below is my code for this.

require 'tmpdir' require 'tempfile' Dir.mktmpdir do |dir| Dir.chdir(dir) TemFile.new("f") sleep 20 end 

this gives me this exception: Errno :: EACCES: Permission denied - C: / Users / SANJAY ~ 1 / AppData / Local / Temp / d20130724-5600-ka2ame, because ruby ​​is trying to delete the temporary directory, which is not empty.plz help me create a temporary file inside the temp directory.

+4
source share
1 answer

You must use the Tempfile class.

 require 'tempfile' file = Tempfile.new('foo') file.path # => A unique filename in the OS temp directory, # eg: "/tmp/foo.24722.0" # This filename contains 'foo' in its basename. file.write("hello world") file.rewind file.read # => "hello world" file.close file.unlink # deletes the temp file 

To create temporary folders, you can use Dir.mktmpdir .

-one
source

Source: https://habr.com/ru/post/1493179/


All Articles