{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"file.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"sEXFHFRdTnJP","colab_type":"text"},"source":["# file"]},{"cell_type":"markdown","metadata":{"id":"SCuoBnQFToiS","colab_type":"text"},"source":["## [Python Program to Merge Mails](https://www.programiz.com/python-programming/examples/merge-mails)\n","In this program, you'll learn to merge mails into one.\n","\n","When we want to send the same invitations to many people, the body of the mail does not change. Only the name (and maybe address) needs to be changed.\n","\n","Mail merge is a process of doing this. Instead of writing each mail separately, we have a template for body of the mail and a list of names that we merge together to form all the mails.\n","\n","### Source Code to Merge Mails"]},{"cell_type":"code","metadata":{"id":"HWkA24tcTycz","colab_type":"code","colab":{}},"source":["# Python program to mail merger\n","# Names are in the file names.txt\n","# Body of the mail is in body.txt\n","# open names.txt for reading\n","with open(\"names.txt\",'r',encoding = 'utf-8') as names_file:\n"," # open body.txt for reading\n"," with open(\"body.txt\",'r',encoding = 'utf-8') as body_file:\n"," \n"," # read entire content of the body\n"," body = body_file.read()\n"," # iterate over names\n"," for name in names_file:\n"," mail = \"Hello \"+name+body\n"," # write the mails to individual files\n"," with open(name.strip()+\".txt\",'w',encoding = 'utf-8') as mail_file:\n"," mail_file.write(mail)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"7Ma0gz4TT0lZ","colab_type":"text"},"source":["For this program, we have written all the names in separate lines in the file \"names.txt\". The body is in the \"body.txt\" file.\n","\n","\n","We open both the files in reading mode and iterate over each name using a for loop. A new file with the name \"[name].txt\" is created, where name is the name of that person.\n","\n","We use strip() method to clean up leading and trailing whitespaces (reading a line from the file also reads the newline '\\n' character). Finally, we write the content of the mail into this file using the write() method.\n","\n","Learn more about [files in Python.](https://www.programiz.com/python-programming/file-operation)"]},{"cell_type":"markdown","metadata":{"id":"5dYUSXBpT7oa","colab_type":"text"},"source":["## Python Program to Find the Size (https://www.programiz.com/python-programming/examples/resolution-image)\n","You will learn to find resolution of a jpeg image in this example without using external libraries\n","\n","JPEG (pronounced \"jay-peg\") stands for Joint Photographic Experts Group. It is one of the most widely used compression techniques for image compression.\n","\n","Most of the file formats have headers (initial few bytes) which contain useful information about the file.\n","\n","For example, jpeg headers contain information like height, width, number of color (grayscale or RGB) etc. In this program, we find the resolution of a jpeg image reading these headers, without using any external library.\n","\n","### Source Code of Find Resolution of JPEG Image"]},{"cell_type":"code","metadata":{"id":"68lwGFoJUBz4","colab_type":"code","colab":{}},"source":["def jpeg_res(filename):\n"," \"\"\"\"This function prints the resolution of the jpeg image file passed into it\"\"\"\n"," # open image for reading in binary mode\n"," with open(filename,'rb') as img_file:\n"," # height of image (in 2 bytes) is at 164th position\n"," img_file.seek(163)\n"," # read the 2 bytes\n"," a = img_file.read(2)\n"," # calculate height\n"," height = (a[0] << 8) + a[1]\n"," # next 2 bytes is width\n"," a = img_file.read(2)\n"," # calculate width\n"," width = (a[0] << 8) + a[1]\n"," print(\"The resolution of the image is\",width,\"x\",height)\n","jpeg_res(\"img1.jpg\")"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"1WvXEKzjUDFi","colab_type":"text"},"source":["In this program, we opened the image in binary mode. Non-text files must be open in this mode. The height of the image is at 164th position followed by width of the image. Both are 2 bytes long.\n","\n","Note that this is true only for JPEG File Interchange Format (JFIF) standard. If your image is encode using other standard (like EXIF), the code will not work.\n","\n","We convert the 2 bytes into a number using bitwise shifting operator <<. Finally, the resolution is displayed."]},{"cell_type":"markdown","metadata":{"id":"TpUqsl6oUE9f","colab_type":"text"},"source":["## [Python Program to Find Hash of File](https://www.programiz.com/python-programming/examples/hash-file)\n","In this article, you'll learn to find the hash of a file and display it.\n","\n","Hash functions take an arbitrary amount of data and return a fixed-length bit string. The output of the function is called the digest message.\n","\n","They are widely used in cryptography for authentication purposes. There are many hashing functions like MD5, SHA-1 etc. Refer this page to know more about hash functions in cryptography.\n","\n","In this example, we will illustrate how to hash a file. We will use the SHA-1 hashing algorithm. The digest of SHA-1 is 160 bits long.\n","\n","We do not feed the data from the file all at once, because some files are very large to fit in memory all at once. Breaking the file into small chunks will make the process memory efficient.\n","\n","###Source Code to Find Hash"]},{"cell_type":"code","metadata":{"id":"ZedjLkSYUMOJ","colab_type":"code","colab":{}},"source":["# Python rogram to find the SHA-1 message digest of a file\n","# import hashlib module\n","import hashlib\n","def hash_file(filename):\n"," \"\"\"\"This function returns the SHA-1 hash\n"," of the file passed into it\"\"\"\n"," # make a hash object\n"," h = hashlib.sha1()\n"," # open file for reading in binary mode\n"," with open(filename,'rb') as file:\n"," # loop till the end of the file\n"," chunk = 0\n"," while chunk != b'':\n"," # read only 1024 bytes at a time\n"," chunk = file.read(1024)\n"," h.update(chunk)\n"," # return the hex representation of digest\n"," return h.hexdigest()\n","message = hash_file(\"track1.mp3\")\n","print(message)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"whGpjxMmUN5D","colab_type":"text"},"source":["In this program, we open the file in binary mode. Hash functions are available in the hashlib module. We loop till the end of the file using a while loop. On reaching the end, we get empty bytes object.\n","\n","In each iteration we only read 1024 bytes (this value can be changed according to our wish) from the file and update the hashing function.\n","\n","Finally, we return the digest message in hexadecimal representation using the hexdigest() method."]}]}