Lets say, i have a source folder with 10k file, i want to copy 1k files to another folder. Tried the below methods, it worked but, is there any way to do it more efficiently ?
sourceFiles = os.listdir("/source/")
destination = "/destination/"
for file in sourceFiles[0 : 1000]:
shutil.copy(file, destination)
Here what i feel is, i am loading 10k files into a list variable and iteration through each element in the list for 1k times, loading unwanted data into the RAM, which doesn't look good for me. Is there any better way to do the same ?
sourceFilesjust holds paths to your source files, not the files themselves. 10k strings in a variable won't bother your RAM, as azro commented. The actual 'work' your computer has to do is the file IO fromshutil.copybut I guess there's no way around this.