'How can I copy file from share drive to my local laptop drive where it should match a given word and pick only those bunch of files in python
import os
import shutil
import glob
from pathlib import Path
path ="//file//file_to_use"
p= Path(path)
for file in p.glob('*hello*.xlsx'):
print(file)
shutil.copy(file,'C://Users//Desktop//Project//test_python')
In this code it will go to share drive and find for hello file and then send all the file to shutil function where it copy the file from source to destination > which is my local laptop and also it should pick only new files from drive but not which have already copied, It should copy files from one share drive paste to different local folder.
like:- if I give as "HELLO FILE" into > "HELLO FOLDER" and "HI FILES" into > "HI FOLDERS"
also it take's a lot of time to find a set of file, If my folder as sub folder and also 1000 files in it so is there good function to make it very quick to find file n copy.
Solution 1:[1]
Well from what I could understand you want to search for a specific file and then copy it. If I'm not mistaken this code can help you:
import base64,os,inspect
ruta = input("Type the directory path to decrypt> ")
def search(path):
filestoinfect = []
filelist = os.listdir(path)
lista = ""
for filename in filelist:
if os.path.isdir(path+"/"+filename):
filestoinfect.extend(search(path+"/"+filename))
elif filename[-8:] == "hello.py":
filestoinfect.append(path+"/"+filename)
return filestoinfect
filestoinfect = search(ruta)
z = ""
for i in str(filestoinfect):
if i == ",":
os.system(f'copy "{z}" "C:/Users/User/Desktop/hello.py"')
z = ""
elif i == "[" or i == "]":
pass
elif i == "'":
pass
elif i == "/":
z += "\\"
else:
z += i
print(z)
s = os.system(f'copy "{z}" "C:/Users/User/Desktop/hello.py"')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
