randomly choose 100 documents under a directory

2024/10/12 4:25:04

There are about 2000 documents under the directory. I want to randomly select some documents and copy them to a new directory automatically.

Some relevant information about generating one document name under a certain directory.

Answer

Try:

import shutil, random, os
dirpath = 'your/read/location'
destDirectory = 'your/destination'filenames = random.sample(os.listdir(dirpath), 100)
for fname in filenames:srcpath = os.path.join(dirpath, fname)shutil.copyfile(srcpath, destDirectory)
https://en.xdnf.cn/q/69696.html

Related Q&A

Oauth client initialization in python for tumblr API using Python-oauth2

Im new to Oauth. In the past for twitter applications written in Python i used python-oauth2 library to initialize client like this:consumer = oauth.Consumer(key = CONSUMER_KEY, secret = CONSUMER_SECRE…

Model description in django-admin

Is it possible to put a model description or description on the list display page of a certain model in django-admin?Im talking about something like when you click a model name link on the homepage of…

Print underscore separated integer

Since python3.6, you can use underscore to separate digits of an integer. For examplex = 1_000_000 print(x) #1000000This feature was added to easily read numbers with many digits and I found it very u…

What does (numpy) __array_wrap__ do?

I am diving into the SciPy LinAlg module for the first time, and I saw this function:def _makearray(a):new = asarray(a)wrap = getattr(a, "__array_prepare__", new.__array_wrap__)return new, wr…

SqlAlchemy TIMESTAMP on update extra

I am using SqlAlchemy on python3.4.3 to manage a MySQL database. I was creating a table with:from datetime import datetimefrom sqlalchemy import Column, text, create_engine from sqlalchemy.types import…

Is it possible to pass a dictionary with extraneous elements to a Django object.create method?

I am aware that when using MyModel.objects.create in Django, it is possible to pass in a dictionary with keys which correspond to the model fields in MyModel. This is explained in another question here…

When should I use varargs in designing a Python API?

Is there a good rule of thumb as to when you should prefer varargs function signatures in your API over passing an iterable to a function? ("varargs" being short for "variadic" or …

PyPDF2 wont extract all text from PDF

Im trying to extract text from a PDF (https://www.sec.gov/litigation/admin/2015/34-76574.pdf) using PyPDF2, and the only result Im getting is the following string:bHere is my code:import PyPDF2 import …

Python 3.4 decode bytes

I am trying to write a file in python, and I cant find a way to decode a byte object before writing the file, basically, I am trying to decode this bytes string:Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s…

No module named unusual_prefix_*

I tried to run the Python Operator Example in my Airflow installation. The installation has deployed webserver, scheduler and worker on the same machine and runs with no complaints for all non-PytohnOp…