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>You need to make sure <code>FileName</code> either <em>equals</em> the file's name <em>or startswith</em> the file's name + a dot. Else you cannot exclude errors with possible files without extensions, or files with the same <em>partial</em> name.</p>

<p>Furthermore I'd suggest to use a conditional rather then <code>try</code>/<code>except</code>; only do the action if the file actually exists, else retry the input:</p>

<pre class="lang-py prettyprint-override"><code>import shutil, os, time
while True:
    dr = 'C:\\users\\guest\\desktop\\Folder\\'
    FileName = input('\n Please enter the file name: ')
    # see if the file exists (listing files matching the conditions)
    match = [f for f in os.listdir(dr) if any([f == FileName, f.startswith(FileName+".")])]
    # if the list is not empty, use the first match, copy it and break
    if match:
        match = match[0]
        shutil.copy(os.path.join(dr, match), os.path.join('C:\\users\\guest\\desktop\\employees\\Folder1', match))
        # etc...
        break
    else:
        print("No such file exists.  Please try again.")

print("File transfer complete.")
time.sleep(2)
quit()
</code></pre>

python python-3.x