1

I need to copy a file that contains some string, call it mystring, in the filename from a directory to another. At the moment I use

for file in glob.glob(FileNameM):
        shutil.copy(file, dest);

where dest is the destination directory and

FileNameM = source_directory + some_known_string + mystring + "*"

This code works fine when there is an extension only after mystring, but if there is something else plus an extension the code fails.

Example FileNameM that works:

/home/eugene/GDrive/Project-related/Skyrmion_Lattice_Output/Output_Good_Enough/Parameters__triangular_N_225_kappa_1.0_beta_0.11_a_3.63311050137*

and leads to the file

/home/eugene/GDrive/Project-related/Skyrmion_Lattice_Output/Output_Good_Enough/Parameters__triangular_N_225_kappa_1.0_beta_0.11_a_3.63311050137.dat

Example FileNameM that doesn't work:

/home/eugene/GDrive/Project-related/Skyrmion_Lattice_Output/Output/Parameters__triangular_N_225_kappa_1.0_beta_0.12_a_3.6341634222*

and this shall lead to the file

/home/eugene/GDrive/Project-related/Skyrmion_Lattice_Output/Output/Parameters__triangular_N_225_kappa_1.0_beta_0.12_a_3.63416342223.dat

Do you have any idea how can I improve?

2
  • Could you show an example of a path that works and of a path that does not to help to reproduce your problem? Commented Dec 13, 2015 at 17:01
  • Added examples to the main body Commented Dec 13, 2015 at 17:20

1 Answer 1

2

I just tried it with your example filenames and got both filenames by glob.glob(), even with more characters missing:

import glob
    FileNameM = 'test\Parameters__triangular_N_225_kappa_1.0_beta_0.12_a_3.6341634*'
    for file in glob.glob(FileNameM):
        print(file)
# test\Parameters__triangular_N_225_kappa_1.0_beta_0.12_a_3.63416342223.dat

Running on Windows; a dummy file was in the subfolder test next to the script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.