Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Examples of useful file utilities"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "## The os library for listing files and making directories"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {},
  21. "outputs": [],
  22. "source": [
  23. "import os"
  24. ]
  25. },
  26. {
  27. "cell_type": "markdown",
  28. "metadata": {},
  29. "source": [
  30. "Say I have a directory on my computer filled with *.ab1* files that I need to work with."
  31. ]
  32. },
  33. {
  34. "cell_type": "code",
  35. "execution_count": 2,
  36. "metadata": {},
  37. "outputs": [],
  38. "source": [
  39. "abi_dir = '/Users/triznam/Downloads/rev'"
  40. ]
  41. },
  42. {
  43. "cell_type": "markdown",
  44. "metadata": {},
  45. "source": [
  46. "You can then use os.listdir to get a list of all of the files in that directory."
  47. ]
  48. },
  49. {
  50. "cell_type": "code",
  51. "execution_count": 3,
  52. "metadata": {},
  53. "outputs": [],
  54. "source": [
  55. "for file in os.listdir(abi_dir):\n",
  56. " print(file)"
  57. ]
  58. },
  59. {
  60. "cell_type": "markdown",
  61. "metadata": {},
  62. "source": [
  63. "Notice Macs includes this weird \".DS_Store\" file in every folder, so you should build in some sort of filter."
  64. ]
  65. },
  66. {
  67. "cell_type": "code",
  68. "execution_count": 4,
  69. "metadata": {},
  70. "outputs": [],
  71. "source": [
  72. "for file in os.listdir(abi_dir):\n",
  73. " if file.endswith('.ab1'):\n",
  74. " print(file)"
  75. ]
  76. },
  77. {
  78. "cell_type": "markdown",
  79. "metadata": {},
  80. "source": [
  81. "Now, say for example, you want to move all of these files to another directory. First, let's make a new directory with *os.makedirs*. This command is similar to `mkdir -p` in Unix, where is automatically creates parent directories if needed, so I used this instead of *os.mkdir*.\n",
  82. "\n",
  83. "I also include a check to see if the directory already exists, since it will error out if it has already been made. I honestly didn't realize this until I got through writing this up, and then tried Restart and Run All on the notebook."
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 5,
  89. "metadata": {},
  90. "outputs": [],
  91. "source": [
  92. "good_abis_path = '/Users/triznam/Documents/ab1_files/good_ones/'\n",
  93. "if not os.path.exists(good_abis_path):\n",
  94. " os.makedirs(good_abis_path)"
  95. ]
  96. },
  97. {
  98. "cell_type": "markdown",
  99. "metadata": {},
  100. "source": [
  101. "## Copying files with shutil"
  102. ]
  103. },
  104. {
  105. "cell_type": "markdown",
  106. "metadata": {},
  107. "source": [
  108. "For some reason, the command to copy files is in a different library than os 🤷‍♂️, so we'll have to import the *shutil* library."
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": 6,
  114. "metadata": {},
  115. "outputs": [],
  116. "source": [
  117. "import shutil"
  118. ]
  119. },
  120. {
  121. "cell_type": "markdown",
  122. "metadata": {},
  123. "source": [
  124. "Now let's run through the same for-loop as before to copy each file to the new directory. The *shutil.copyfile* command requires the full path, so we'll also need to use *os.path.join* to combine the directory with the file name."
  125. ]
  126. },
  127. {
  128. "cell_type": "code",
  129. "execution_count": 7,
  130. "metadata": {},
  131. "outputs": [
  132. {
  133. "name": "stdout",
  134. "output_type": "stream",
  135. "text": [
  136. "Current filepath: /Users/triznam/Downloads/rev/647767_HCO2198_E01.ab1\n",
  137. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647767_HCO2198_E01.ab1\n",
  138. "--------\n",
  139. "Current filepath: /Users/triznam/Downloads/rev/647766_HCO2198_C01.ab1\n",
  140. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647766_HCO2198_C01.ab1\n",
  141. "--------\n",
  142. "Current filepath: /Users/triznam/Downloads/rev/647768_HCO2198_F01.ab1\n",
  143. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647768_HCO2198_F01.ab1\n",
  144. "--------\n",
  145. "Current filepath: /Users/triznam/Downloads/rev/647769_HCO2198_G01.ab1\n",
  146. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647769_HCO2198_G01.ab1\n",
  147. "--------\n",
  148. "Current filepath: /Users/triznam/Downloads/rev/647764_HCO2198_A01.ab1\n",
  149. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647764_HCO2198_A01.ab1\n",
  150. "--------\n",
  151. "Current filepath: /Users/triznam/Downloads/rev/647762_HCO2198_D01.ab1\n",
  152. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647762_HCO2198_D01.ab1\n",
  153. "--------\n",
  154. "Current filepath: /Users/triznam/Downloads/rev/647765_HCO2198_B01.ab1\n",
  155. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647765_HCO2198_B01.ab1\n",
  156. "--------\n",
  157. "Current filepath: /Users/triznam/Downloads/rev/647763_HCO2198_B07.ab1\n",
  158. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647763_HCO2198_B07.ab1\n",
  159. "--------\n",
  160. "Current filepath: /Users/triznam/Downloads/rev/647770_HCO2198_H01.ab1\n",
  161. "New filepath: /Users/triznam/Documents/ab1_files/good_ones/647770_HCO2198_H01.ab1\n",
  162. "--------\n"
  163. ]
  164. }
  165. ],
  166. "source": [
  167. "for file in os.listdir(abi_dir):\n",
  168. " if file.endswith('.ab1'):\n",
  169. " current_file_path = os.path.join(abi_dir, file)\n",
  170. " print('Current filepath: {}'.format(current_file_path))\n",
  171. " new_file_path = os.path.join(good_abis_path, file)\n",
  172. " print('New filepath: {}'.format(new_file_path))\n",
  173. " shutil.copyfile(current_file_path, new_file_path)\n",
  174. " print('-'*8)"
  175. ]
  176. },
  177. {
  178. "cell_type": "markdown",
  179. "metadata": {},
  180. "source": [
  181. "Let's do a `ls` command to manually check that the files are now in the new directory."
  182. ]
  183. },
  184. {
  185. "cell_type": "code",
  186. "execution_count": 8,
  187. "metadata": {},
  188. "outputs": [
  189. {
  190. "name": "stdout",
  191. "output_type": "stream",
  192. "text": [
  193. "647762_HCO2198_D01.ab1 647765_HCO2198_B01.ab1 647768_HCO2198_F01.ab1\r\n",
  194. "647763_HCO2198_B07.ab1 647766_HCO2198_C01.ab1 647769_HCO2198_G01.ab1\r\n",
  195. "647764_HCO2198_A01.ab1 647767_HCO2198_E01.ab1 647770_HCO2198_H01.ab1\r\n"
  196. ]
  197. }
  198. ],
  199. "source": [
  200. "!ls ~/Documents/ab1_files/good_ones"
  201. ]
  202. },
  203. {
  204. "cell_type": "markdown",
  205. "metadata": {},
  206. "source": [
  207. "## Zipping a directory with shutil"
  208. ]
  209. },
  210. {
  211. "cell_type": "markdown",
  212. "metadata": {},
  213. "source": [
  214. "There is a *zipfile* library in the Python standard library, but I just learned while Googling that there's an easier version right in shutil which is more straightforward, so let's use that."
  215. ]
  216. },
  217. {
  218. "cell_type": "markdown",
  219. "metadata": {},
  220. "source": [
  221. "Say I wanted to zip up the \"good\" ab1 files and put it on the Desktop, I would use the following command:"
  222. ]
  223. },
  224. {
  225. "cell_type": "code",
  226. "execution_count": 10,
  227. "metadata": {},
  228. "outputs": [
  229. {
  230. "data": {
  231. "text/plain": [
  232. "'/Users/triznam/Desktop/good_ones.zip'"
  233. ]
  234. },
  235. "execution_count": 10,
  236. "metadata": {},
  237. "output_type": "execute_result"
  238. }
  239. ],
  240. "source": [
  241. "shutil.make_archive('/Users/triznam/Desktop/good_ones', #The filepath of the zipfile (minus .zip)\n",
  242. " 'zip', #What we want to zip it with -- also can use \n",
  243. " good_abis_path)"
  244. ]
  245. },
  246. {
  247. "cell_type": "code",
  248. "execution_count": null,
  249. "metadata": {},
  250. "outputs": [],
  251. "source": []
  252. }
  253. ],
  254. "metadata": {
  255. "kernelspec": {
  256. "display_name": "Python 3",
  257. "language": "python",
  258. "name": "python3"
  259. },
  260. "language_info": {
  261. "codemirror_mode": {
  262. "name": "ipython",
  263. "version": 3
  264. },
  265. "file_extension": ".py",
  266. "mimetype": "text/x-python",
  267. "name": "python",
  268. "nbconvert_exporter": "python",
  269. "pygments_lexer": "ipython3",
  270. "version": "3.7.1"
  271. }
  272. },
  273. "nbformat": 4,
  274. "nbformat_minor": 2
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement