Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a file only if it doesn't exist

Tags:

posix

haskell

In C, I can call open with O_CREAT and O_EXCL to create a new file if and only if there isn't currently one by that name. I haven't been able to find a way to do that in Haskell. I would prefer something friendly that provides a Handle, ideally with all the tricky exception handling done for me. (I don't necessarily expect it to be done for me correctly, but that's another story.)

like image 269
dfeuer Avatar asked Nov 25 '21 02:11

dfeuer


People also ask

Which mode creates a new file if the file does not exist?

"w" - Write - will create a file if the specified file does not exist.

How do you create a file if it doesn't exist Python?

To create a file if not exist in Python, use the open() function. The open() is a built-in Python function that opens the file and returns it as a file object. The open() takes the file path and the mode as input and returns the file object as output.

How do you create a file if it doesn't exist in shell script?

If you don't care about the mtime, you could use >>/Scripts/file. txt . This will open the file for appending and create it if it doesn't exist.

Will Fopen create a file if not exist?

The fopen function creates the file if it does not exist.

How to create a file if not exist in Python?

The open () method creates a file if it does not exists. We need to pass the required mode value. We will understand Ways to create a file if not exist in Python What is Python Open ()?

What happens if the file does not exist?

If the file does not exist, it creates a new file for reading and writing. It will not truncate the file and append the content at end of the file as shown in the example below. view source print? view source print?

How to create a file if not exists in PowerShell?

Create a File If Not Exists via PowerShell. We can check if a file exist or not by using the PowerShell cmdlet Test-Path and create new file using New-Item cmdlet. The below powershell script will check whether the file sample.txt is already exists or not under the path C:Share. It creates new file if it not exists already ...

How to open a file if the file path doesn't exist?

Seems like if the file path doesn't exist you could also open it in 'x' mode since you've already established that the file doesn't exist. – Adam Richard Oct 2 at 17:27 1 Needless to say that import osmust be declared before using this two-liner. – johann_ka Oct 26 at 16:48 Add a comment | 15


Video Answer


1 Answers

I think this isn’t available directly in base, but you can use openFd from the unix package, setting the exclusive flag in the OpenFileFlags record to True. Summary:

openFd
  path
  WriteOnly
  (Just defaultMode)
  defaultFileFlags { exclusive = True }

The result of this can be converted to a normal Haskell I/O Handle using fdToHandle. If it fails, it should throw an IOError exception, which (I think) you can test with isAlreadyExistsError.

On Windows, this can be done with createFile from the Win32 package.

createFile
  path
  gENERIC_WRITE
  fILE_SHARE_NONE
  Nothing
  cREATE_NEW
  fILE_ATTRIBUTE_NORMAL
  Nothing

You can then use hANDLEToHandle to get a regular Handle. I’m not sure how the error handling works here; you may need to check the HANDLE against nullPtr and call getLastError to get error info.

like image 99
Jon Purdy Avatar answered Oct 24 '22 06:10

Jon Purdy