0

I have a QDialogBox where there is an option to upload a file. I can upload files and save them to a folder. It works fine.

But if in case there is a file that already exists in the folder, I am not sure how to handle that scenario.

I want to warn the user that the file with same name already exists.

Is there a Windows API that I can use in this case? (because when we manually save an existing file, we get a warning, how can I use that?)

If someone can point me to that documentation, it will be great.

Ejaz
  • 1,504
  • 3
  • 25
  • 51

3 Answers3

2

If you are using a QFileDialog, confirmOverwrite is activated by default, so, if getSaveFileName() returned a non-empty QString, then that means the user accepted to overwrite the file. Other way, you get an empty QString. Then, you can check if the file exists, and remove it in that case, but you know that the user was Ok with that.

Smasho
  • 1,170
  • 8
  • 19
  • You can check for the file as @purplehuman said, or the python way: `import os; os.path.exists(filename)` – Smasho Feb 06 '15 at 20:40
1

There is always a potential race condition when saving files. Checking to see if the file exists first is not safe, because some other process could create a file with the same name in between the check and when you actually write the file.

To avoid problems, the file must be opened with exclusive access, and in such a way that it immediately fails if it already exists.

If you want to do things properly, take a look at these two answers:

Community
  • 1
  • 1
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you. I can work with text files. But is there a different way to work with rich text files? I have a word doc, it gets corrupted this way. – Ejaz Feb 07 '15 at 16:04
  • @PEJK. You probably need to write the file in binary mode: i.e. `os.fdopen(fd, "wb")`. – ekhumoro Feb 07 '15 at 18:53
0

You can use QDir::entryList() to get the file names in a directory if you're not using a QFileDialog.

 QDir dir("/path/to/directory");
 QStringList fileNames = dir.entryList();

Then iterating through file names, you can see if there's a file with the same name. If you need it, I can give an example for that too. It'd be C++, but easily adaptable to Python.

Edit: Smasho just suggested that using QDir::exists() method. You can check if the file name exists in the directory with this method instead of iterating like I suggested.

 if(dir.exists(uploadedFileName))
  • Don't need to iterate, QDir has a exists() method – Smasho Feb 06 '15 at 20:06
  • @Smasho That's right, but isn't it to check if "dir" exists? –  Feb 06 '15 at 20:07
  • 1
    It has both, exists() without arguments, and with a string argument [http://qt-project.org/doc/qt-4.8/qdir.html#exists]. With an argument, it refers to a file **inside** that dir – Smasho Feb 06 '15 at 20:08
  • @Smasho Thank you for the info. Updated the answer. I was using exists() method, but I didn't know there was another one with a QString argument. –  Feb 06 '15 at 20:13