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

Prevent crash if folder already exists #2867

Merged
merged 2 commits into from
Sep 1, 2020

Conversation

jcohenadad
Copy link
Member

When launching multiple sct_run_batch in parallel using run_all with the same output folder, the following error sometimes happens:

Spinal Cord Toolbox (git-master-253936cfc07712c587d9f4cfbbcb760f151172f0)

/home/jcohen/sct/scripts/sct_run_batch.py:241: UserWarning: Using the `-config|-c` flag with additional arguments is discouraged
  warnings.warn(UserWarning('Using the `-config|-c` flag with additional arguments is discouraged'))
configuring
Traceback (most recent call last):
  File "/home/jcohen/sct/scripts/sct_run_batch.py", line 479, in <module>
    main(sys.argv[1:])
  File "/home/jcohen/sct/scripts/sct_run_batch.py", line 300, in main
    os.mkdir(pth)
FileExistsError: [Errno 17] File exists: '/scratch/jcohen/results_csa_t1/results'

This PR fixes the problem by changing: os.mkdir(pth) for os.mkdir(pth, exist_ok=True)

Fixes #2866

@jcohenadad jcohenadad added enhancement category: improves performance/results of an existing feature sct_run_batch context: labels Sep 1, 2020
@jcohenadad jcohenadad added this to the 5.0.0 milestone Sep 1, 2020
@jcohenadad
Copy link
Member Author

jcohenadad commented Sep 1, 2020

oops!

/home/jcohen/sct/scripts/sct_run_batch.py:241: UserWarning: Using the `-config|-c` flag with additional arguments is discouraged
  warnings.warn(UserWarning('Using the `-config|-c` flag with additional arguments is discouraged'))
configuring
Traceback (most recent call last):
  File "/home/jcohen/sct/scripts/sct_run_batch.py", line 481, in <module>
    main(sys.argv[1:])
  File "/home/jcohen/sct/scripts/sct_run_batch.py", line 299, in main
    os.mkdir(pth, exist_ok=True)
TypeError: 'exist_ok' is an invalid keyword argument for this function

ah! https://docs.python.org/3.2/library/os.html#os.makedirs

@PaulBautin
Copy link
Member

@jcohenadad, up to now i only tested if the sbatch batch script worked, i am not sure i can configure slurm without connecting to a cluster. But it should work, I'm just wondering if makedirs does not make using exist_ok=True redundant. https://stackoverflow.com/questions/13819496/what-is-different-between-makedirs-and-mkdir-of-os

@jcohenadad
Copy link
Member Author

@jcohenadad, up to now i only tested if the sbatch batch script worked, i am not sure i can configure slurm without connecting to a cluster. But it should work, I'm just wondering if makedirs does not make using exist_ok=True redundant. https://stackoverflow.com/questions/13819496/what-is-different-between-makedirs-and-mkdir-of-os

@PaulBautin i'm not sure what post you are referring to in the URL, but this is the reason i switched for makedirs:

(Can not comment, just add to NPE's answer.)

In Python3, os.makedirs has a default parameter exist_ok=False.
If you set it to True, then os.makedirs will not throw any exception if the leaf exists.
(While os.mkdir doesn't have this parameter.)

Just like this:

os.makedirs('dirA', exist_ok=True)

@PaulBautin
Copy link
Member

@jcohenadad you are right. So unless you want a test with another configuration of slurm, it is all good for me.

@jcohenadad
Copy link
Member Author

@jcohenadad you are right. So unless you want a test with another configuration of slurm, it is all good for me.

if it's all good you need to approve the review so i can merge

@jcohenadad jcohenadad merged commit 004b326 into master Sep 1, 2020
Drulex pushed a commit to Drulex/spinalcordtoolbox that referenced this pull request Sep 30, 2020
* Update sct_run_batch.py

* Replaced mkdir by makedirs
@joshuacwnewton joshuacwnewton deleted the jca/2866-run-batch-FileExistsError branch May 2, 2023 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement category: improves performance/results of an existing feature sct_run_batch context:
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FileExistsError: when running multiple instances of sct_run_batch in parallel
2 participants