Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing to file : Can't open file (CSV.CSVError.cannotOpenFile: file) #110

Open
armaghan84 opened this issue Mar 8, 2020 · 7 comments
Open

Comments

@armaghan84
Copy link


Screen Shot 2020-03-08 at 9 32 31 PM

@joris1995
Copy link

Same issue here. Any solution?

@zant
Copy link

zant commented Mar 22, 2020

I had the same problem at first and it was due to the file not existing yet. So I added a condition to create the file if that's the case:

if !FileManager.default.fileExists(atPath: path) {
  if !FileManager.default.createFile(atPath: path, contents: nil, attributes: nil) {
    // throw error or something 
  }
}

Checking if the file is readable or writable could be also useful:

if !FileManager.default.isReadableFile(atPath: path) {
  // stuff
}

@johncpang
Copy link

You have a URL object, just use it directly:

let stream = OutputStream(url: url, append: false)!

@Pasta
Copy link

Pasta commented Jun 10, 2020

same issue.

1 similar comment
@mash3l777
Copy link

same issue.

@willm132
Copy link

willm132 commented Jan 31, 2021

I am having this issue also. Any resolution? I can verify that my file is being created and is readable/writable, but I keep getting the error let csv = try! CSVWriter(stream: stream) --> Fatal error: 'try!' expression unexpectedly raised an error: CSV.CSVError.cannotOpenFile:

When I try this:

if FileManager.default.isWritableFile(atPath: "\(fileURL)") {
     print("File is writable")
} else {
     print("File is read-only")
}

I am getting File is read-only
Is there a way to change file permissions on create or am I doing something wrong?

@timefrancesco
Copy link

timefrancesco commented Mar 22, 2022

What I've done to resolve it is to copy the file to the user cache directory.

Here is what I do from a file picked using a document picker :

public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
        let myURL = url as URL 
        _ = myURL.startAccessingSecurityScopedResource()
        
        let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
        let temporaryFile = cachesDirectory.appendingPathComponent("tmp.csv")
        
        if FileManager.default.fileExists(atPath: temporaryFile.path) {
            try! FileManager.default.removeItem(atPath: temporaryFile.path)
        }
      
        try! FileManager.default.copyItem(at: url, to: temporaryFile)

      //then use temporaryFile as input to CSVReader

       //when finished remember to call 

      myURL.stopAccessingSecurityScopedResource()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants