copy file with any extension
<p>I have only been attempting to code for a few days so please forgive me for any obvious mistakes that I make.  I have searched around for a couple of days now and have not managed to find a solution to my problem, hence why I have decided to post here.</p>

<p>My issue:  I have managed to create a piece of script which will search in a dedicated folder and copy a file, that the user has named themselves, via input(),  into multiple other folders.  My downfall is that I can only get it to copy files with a specific extension e.g. '.docx'.  What I would like the code to be able to do is pick up the named file regardless of it's extension. </p>

<p>Here is the code:</p>

<pre><code>import shutil, os, time
while True:
    FileName = input('\n Please enter the file name: ') + '.docx'
    try:
        shutil.copy('C:\\users\\guest\\desktop\\Folder\\' + FileName, 'C:\\users\\guest\\desktop\\employees\\Folder1')
        shutil.copy('C:\\users\\guest\\desktop\\Folder\\' + FileName, 'C:\\users\\guest\\desktop\\employees\\Folder2')
        shutil.copy('C:\\users\\guest\\desktop\\Folder\\' + FileName, 'C:\\users\\guest\\desktop\\employees\\Folder3')
    except FileNotFoundError:
        print("No such file exists.  Please try again.")
    else:
        break
print("File transfer complete.")
time.sleep(2)
quit()
</code></pre>

<p>Thanks in advance for any help!</p>

<p>This is how i would start going about it. There are definitely things you have to be careful of like multiple files with same name and different extension, multiple dots in files and so on. I am pretty sure those two are taken into account by the script below. Take a look.</p>

<pre><code>import shutil
import os
import time

my_dir = r'C:\users\guest\desktop\Folder'
while True:
    all_files = os.listdir(my_dir)
    all_files_and_extentions = [x.split('.') for x in all_files]
    print(all_files_and_extentions)  # -&gt; [['mftf5_fats_LocBuckling_B-Basis_v1', 'txt'], ['mftf5_fats_static_LSET_C_v1', 'docx']

    FileName = input('\n Please enter the file name: ')
    print(FileName)  # -&gt; mftf5_fats_LocBuckling_B-Basis_v1

    the_file_with_ext = '.'.join([y for x in all_files_and_extentions for y in x if x[0] == FileName])
    print(the_file_with_ext)  # -&gt; mftf5_fats_LocBuckling_B-Basis_v1.txt

    for files in the_file_with_ext:
        try:
            shutil.copy(my_dir + '\\' + files, 'C:\\users\\guest\\desktop\\employees\\Folder1')
            shutil.copy(my_dir + '\\' + files, 'C:\\users\\guest\\desktop\\employees\\Folder2')
            shutil.copy(my_dir + '\\' + files, 'C:\\users\\guest\\desktop\\employees\\Folder3')
        except FileNotFoundError:
            print("No such file exists.  Please try again.")
        else:
            break   
print("File transfer complete.")
time.sleep(2)
quit()
</code></pre>

python python-3.x