FileExistsError: [WinError 183] Cannot create a file when that file already exists: - python-2.7

i when run this test program create the below error.
import shutil
src=r"G:\aaa"
dst=r"F:\zzz"
shutil.copytree(src,dst, symlinks=False, ignore=None)
FileExistsError: [WinError 183] Cannot create a file when that file already exists:
but the the folder of F:\zzz is empty!!!

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False)
dirs_exist_ok dictates whether to raise an exception in case dst or any missing parent directory already exists.
Python 3.8 now have support of dirs_exist_ok parameter. This won't give that error anymore and overwrite the destination folder in case it already exists.
Hence you need to use:
shutil.copytree(src, dst, dirs_exist_ok=True)

"Cannot create a file when that file already exists" is a generic Windows message which is confusing because it's the same for directories or regular files. (Windows isn't known for very helpful error messages, you have to make do with that)
from the online help of shutil.copytree:
>>> help(shutil.copytree)
Help on function copytree in module shutil:
copytree(src, dst, symlinks=False, ignore=None, copy_function=, ignore_dangling_symlinks=False)
Recursively copy a directory tree.
The destination directory must not already exist.
So first time it probably works, but other times you need to perform
shutil.rmtree(dst)
to remove the destination directory prior to copying the tree (note that Windows is annoying with permissions and that files with read-only attribute can choke shutil.rmtree, which I personally copied the code into a custom version (you're encouraged to do so in the online help) to add a os.chmod(path,0o777) prior to deleting regular files.

Related

Using QT and VS run CGAL5.2's official examples

I downloaded the official example which is CGAL-5.2\examples\Triangulation_2 from the website CGAL.org.
I successfully cmake the examples.
Then I opened Triangulation_2_Examples.sln from G:\MyCGAL_code\code_1\Triangulation_2\build by VS2017.
I also successfully built all projects (without errors).
I then click the debugger in VS: the draw triangulation.exe running fine, but the Triangulation_2 Basic_viewer is empty.
I knew someone's successful results has colored triangles in the window called Triangulation_2 Basic_viewer.
Did anyone try this official example before?
The program draw_triangulation_2 takes a filename as input (the file must contains a set of 2D points).
Without parameter, it takes data/triangulation_prog1.cin by default.
Thus the working directory must contains a directory data and this directory must contains a file triangulation_prog1.cin. Otherwise, the triangulation is empty.
To solve your problem, you need either to create the data directory and copy triangulation_prog1.cin in this directory; or give a valid filename as parameter.

Keep getting load error in VS2017: Domain object

Cannot load " file reference ": Domain object with ID " guid " has already been defined in this or another domain model.
The file to be loaded is a DevExpress xpo data model file and the file itself has successfully loaded on at least 3 prior occasions.
I have searched for the ID guid on the whole hard disk without any result.
How can I fix this?
The problem here is, you have already compiled the file. Look around in your build path for a compiled file, and either move or delete it. Whenever a file is already compiled in a location, the compiler tries to use its resources in the compilation of your new program. This then defines things multiple times, giving you this error.

exception of boostlog when date changed to next day

I use boost log by this config.
[Sinks.2]
Filter="%Severity% >= 2"
Destination=TextFile
AutoFlush=true
Format="[%TimeStamp%] [%ThreadID%] <%Severity%> %Message%"
Asynchronous=false
Target="logs"
FileName="logs/quo.%Y%m%dT%H%M%S.%a.%5N.log.detail"
RotationTimePoint="00:00:00"
RotationSize=104857600
MinFreeSpace=4294967296
MaxSize=4294967296
ScanForFiles=All
when date change to next day. my program crash by exception:
terminate called after throwing an instance of
'boost::filesystem::filesystem_error'
what(): boost::filesystem::last_write_time: No such file or directory: "/root/work/hy-trade/bin/debug/logs/quo.20181027T173106.Sat.00000.log.detail"
I check my disk space, find the free space less than MinFreeSpace in config and the file quo.20181027T173106.Sat.00000.log.detail not exists.
how to avoid this exception?
version of boost is 1.67
thank you
It looks like someone had already deleted the log file before it was rotated. It may have been an external process, or Boost.Log.
With Boost.Log, this can happen if you have multiple file sinks that write log files into the same directory, which is also used as the target directory for the rotated files (i.e. the FileName parameter includes the path specified in the Target parameter, and there are multiple sinks that use that path). The problem is because, according to ScanForFiles=All, the library scans the target directory for any files but does not update the file counter to be used for creating new files. This means that if the file "quo.20181027T173106.Sat.00000.log.detail" was present in that directory when your process started then it would be considered as an old file, even if upon starting your process would be still writing new logs to that file. Then, when a file rotation happens and storage limits are exceeded (e.g. if MinFreeSpace is not satisfied), that file may be deleted. The rotation has to happen on another sink that still stores files into the same "logs" directory.
To solve the problem you can do one of the following:
Use ScanForFiles=Matching in your settings so that the file counter is updated after scanning. This will make sure that new log files have unique names and don't get deleted prematurely.
Write log files to a different directory from your target storage. I.e. specify FileName so that it doesn't point to the same directory as Target.
Also, you may want to add exception handling to avoid crashing in case of errors (which may still happen for whatever reason on filesystem operations). See here and here for more info (also, follow the links in those sections).

create a unique temporary directory [duplicate]

This question already has answers here:
How to create a temporary directory in C++?
(6 answers)
Closed 3 years ago.
I'm trying to create a unique temporary directory in the system temp folder and have been reading about the security and file creation issues of tmpnam().
I've written the below code and was wondering whether it would satisfy these issues, is my use of the tmpnam() function correct and the throwing of the filesystem_error? Should I be adding checks for other things (e.g. temp_directory_path, which also throws an exception)?
// Unique temporary directory path in the system temporary directory path.
std::filesystem::path tmp_dir_path {std::filesystem::temp_directory_path() /= std::tmpnam(nullptr)};
// Attempt to create the directory.
if (std::filesystem::create_directories(tmp_dir_path)) {
// Directory successfully created.
return tmp_dir_path;
} else {
// Directory could not be created.
throw std::filesystem_error("directory could not be created.");
}
From cppreference.com:
Although the names generated by std::tmpnam are difficult to guess, it is possible that a file with that name is created by another process between the moment std::tmpnam returns and the moment this program attempts to use the returned name to create a file.
The problem is not with how you use it, but the fact that you do.
For example, in your code sample, if a malicious user successfully guesses and creates the directory right in between the first and second line, it might deny service (DOS) from your application, which might be critical, or not.
Instead, there is a way to do this without races on POSIX-compliant systems:
For files see mkstemp(3) for more information
For dirs see mkdtemp(3) for more information.
Your code is fine. Because you try to create the directory the OS will arbitrate between your process and another process trying to create the same file so, if you win, you own the file and if you lose you get an error.
I wrote a similar function recently. Whether you throw an exception or not depends on how you want to use this function. You could, for example simply return either an open or closed std::fstream and use std::fstream::is_open as a measure of success or return an empty pathname on failure.
Looking up std::filesystem::create_directories it will throw its own exception if you don't supply a std::error_code parameter so you don't need to throw your own exception:
std::filesystem::path tmp_dir_path {std::filesystem::temp_directory_path() /= std::tmpnam(nullptr)};
// Attempt to create the directory.
std::filesystem::create_directories(tmp_dir_path));
// If that failed an exception will have been thrown
// so no need to check or throw your own
// Directory successfully created.
return tmp_dir_path;

Immuconf with Clojure not handling tree config files

Whenever I add a third config file to my .immuconf.edn I get:
No configuration files were specified, and neither an .immuconf.edn file nor
an IMMUCONF_CFG environment variable was found
This is driving me crazy since I cant really find anything wrong.
Using this loads thing OK:
["configs/betfair.edn" "configs/web-server.edn"]
however this generated an error:
["configs/betfair.edn" "configs/web-server.edn" "~/betfair.edn"]
This is the content of betfair.edn
{:betfair {:usr "..."
:pwd "..."
:app-key "..." ;; key used
:app-key-live "..."
:app-key-test "..."}}
(where ... is replaced with actual strings)
Why am I getting this error when adding the third file and how can I fix this?
Make sure that the last file specified in your <project dir>/.immuconf.edn (~/betfair.edn) exists in your home directory.
Immuconf does some magic to replace ~ in filenames specified in .immuconf.edn with a value of (System/getProperty "user.home") so you might check if that system property points to the same directory where your ~/betfair.edn file is located.
I have recreated your setup and it works on my machine so it is probably a problem with locations or access rights to your files. Unfortunately, error handling for the no arg invocation of (immuconf.config/load) doesn't help in troubleshooting as it swallows any exceptions and returns nil. That exception would probably tell you what kind of error occured (some file not found or some IO error happened). You might want to file a pull request with a patch to log such errors as warnings instead of ignoring them.