Skip to content

Instantly share code, notes, and snippets.

@AashishTiwari
Created March 22, 2018 08:15
Show Gist options
  • Save AashishTiwari/72039603f18dc4f2de075b6c0b88f002 to your computer and use it in GitHub Desktop.
Save AashishTiwari/72039603f18dc4f2de075b6c0b88f002 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from pyPdf import PdfFileReader\n",
"import shutil"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"pdf_dir = r'/home/user/Desktop/finance_2016_17/'\n",
"missed_files = []\n",
"pdf_files = []\n",
"for doc in os.listdir(pdf_dir):\n",
" if doc.endswith(\".pdf\"):\n",
" try:\n",
" pdf = PdfFileReader(open(os.path.join(pdf_dir, doc)))\n",
" pdf_files.append([doc, pdf.getNumPages()])\n",
" except:\n",
" missed_files.append(doc)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['PSI_G&A_16-17_655.pdf',\n",
" 'PSI_G&A_16-17_1401-1.pdf',\n",
" 'PSI_G&A_16-17_861.pdf',\n",
" 'PSI_G&A_16-17_1596-1.pdf',\n",
" 'PSI_G&A_16-17_441.pdf',\n",
" 'PSI_G&A_16-17_1491-1.pdf']"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"missed_files"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"a = sorted(pdf_files, key=lambda x: (x[1], x[0]), reverse=True)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"mv = [val for idx, val in enumerate(a) if val[1]> 100]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"big_dir = r'/home/user/Desktop/big_documents/'\n",
"for idx,val in enumerate(mv):\n",
" try:\n",
" shutil.move(os.path.join(pdf_dir, val[0]), os.path.join(big_dir, val[0]))\n",
" except:\n",
" print val"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"24"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(mv)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment