{"id":3496,"date":"2020-07-09T16:30:42","date_gmt":"2020-07-09T11:00:42","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3496"},"modified":"2026-07-13T12:27:11","modified_gmt":"2026-07-13T06:57:11","slug":"python-truncate","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-truncate\/","title":{"rendered":"Python Truncate: Files, Numbers, Strings, and Lists"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> Python uses different truncation tools for different data: file.truncate() changes an open file&#8217;s length, os.truncate() works from a path, math.trunc() removes a number&#8217;s fractional part toward zero, and slicing keeps a shorter string or list. Check whether the operation mutates existing data before using it in production.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate.png\" alt=\"Python Pool infographic comparing file truncate os.truncate math.trunc and slicing for strings and lists\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>Truncation means different operations for files, numbers, and sequences; choose the API that matches the data type and verify whether it mutates existing data.<\/figcaption><\/figure>\n<p>Python truncation means cutting data down to a shorter size. For files, <code>truncate()<\/code> changes the file length. For numbers, <code>math.trunc()<\/code> removes the fractional part. For strings and lists, slicing is the usual way to keep only part of the value.<\/p>\n<p>The important detail is that each kind of data has its own truncation tool. File objects have <code>file.truncate()<\/code>, the <code>os<\/code> module has <code>os.truncate()<\/code> for paths, numbers use <code>math.trunc()<\/code>, and strings use slice syntax. Mixing these up leads to confusing code and sometimes data loss.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Use_filetruncate_to_Resize_an_Open_File\" >Use file.truncate() to Resize an Open File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Truncate_at_the_Current_File_Position\" >Truncate at the Current File Position<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Truncate_Binary_Files_Carefully\" >Truncate Binary Files Carefully<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Use_ostruncate_When_You_Have_a_Path\" >Use os.truncate() When You Have a Path<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Use_mathtrunc_for_Numbers\" >Use math.trunc() for Numbers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Use_Slicing_to_Truncate_Strings\" >Use Slicing to Truncate Strings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Checklist\" >Checklist<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#References\" >References<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Truncate_An_Open_File_Deliberately\" >Truncate An Open File Deliberately<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Use_ostruncate_For_A_Path\" >Use os.truncate For A Path<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Truncate_Numbers_Toward_Zero\" >Truncate Numbers Toward Zero<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Slice_Strings_And_Lists\" >Slice Strings And Lists<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#What_does_filetruncate_do\" >What does file.truncate() do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#What_is_mathtrunc_used_for\" >What is math.trunc() used for?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#How_do_I_truncate_a_Python_string\" >How do I truncate a Python string?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/#What_is_the_difference_between_filetruncate_and_ostruncate\" >What is the difference between file.truncate() and os.truncate()?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_filetruncate_to_Resize_an_Open_File\"><\/span>Use file.truncate() to Resize an Open File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>truncate()<\/code> method on a file object resizes that file. Pass the target size in characters for a text file or bytes for a binary file. The example below writes ten characters and then keeps only the first five.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\n\npath = Path(\"notes.txt\")\npath.write_text(\"abcdefghij\", encoding=\"utf-8\")\n\nwith path.open(\"r+\", encoding=\"utf-8\") as file:\n    file.truncate(5)\n\nprint(path.read_text(encoding=\"utf-8\"))\n<\/code><\/pre>\n<\/div>\n<p>The file now contains <code>abcde<\/code>. Truncation is destructive, so use it only when you are sure the removed data is no longer needed. If the file may not exist yet, see PythonPool&#8217;s guide to <a href=\"https:\/\/www.pythonpool.com\/python-check-if-file-exists\/\">checking whether a file exists in Python<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Truncate_at_the_Current_File_Position\"><\/span>Truncate at the Current File Position<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you call <code>truncate()<\/code> without a size, Python uses the current file position. Move the cursor with <code>seek()<\/code> first when you want to cut the file at a specific point.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\n\npath = Path(\"notes.txt\")\npath.write_text(\"abcdefghij\", encoding=\"utf-8\")\n\nwith path.open(\"r+\", encoding=\"utf-8\") as file:\n    file.seek(3)\n    file.truncate()\n\nprint(path.read_text(encoding=\"utf-8\"))\n<\/code><\/pre>\n<\/div>\n<p>This keeps the first three characters. The relationship between <code>seek()<\/code> and <code>truncate()<\/code> matters when you are updating logs, fixed-width files, or temporary files in place. It also makes the code&#8217;s intent explicit: first choose the cut point, then resize the file at that point.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/truncate-file-b217.png\" alt=\"Python Pool infographic showing a file, open write mode, truncate size, and resulting length\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>A file can be shortened or extended to a chosen byte length with truncate semantics.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Truncate_Binary_Files_Carefully\"><\/span>Truncate Binary Files Carefully<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For binary files, the size is measured in bytes. That is useful for binary formats, cached data, and generated files, but it can corrupt a file if you cut through the middle of a structured record.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\n\npath = Path(\"data.bin\")\npath.write_bytes(b\"abcdef\")\n\nwith path.open(\"r+b\") as file:\n    file.truncate(4)\n\nprint(path.read_bytes())\n<\/code><\/pre>\n<\/div>\n<p>If you are intentionally writing bytes, PythonPool&#8217;s <a href=\"https:\/\/www.pythonpool.com\/write-bytes-to-file-python\/\">write bytes to a file<\/a> guide covers binary mode and byte strings in more detail. For text files with non-ASCII characters, remember that character count and byte count may not be the same under UTF-8.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_ostruncate_When_You_Have_a_Path\"><\/span>Use os.truncate() When You Have a Path<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>os.truncate()<\/code> resizes a file by path, so you do not need to keep a file object open. It is a direct tool for scripts that already know the target path and size.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import os\nfrom pathlib import Path\n\npath = Path(\"notes.txt\")\npath.write_text(\"abcdefghij\", encoding=\"utf-8\")\n\nos.truncate(path, 2)\n\nprint(path.read_text(encoding=\"utf-8\"))\n<\/code><\/pre>\n<\/div>\n<p>This leaves only the first two characters. If you need to create the file before resizing it, see <a href=\"https:\/\/www.pythonpool.com\/python-touch-file\/\">Python touch file<\/a> for pathlib-based creation patterns. Prefer <code>pathlib<\/code> for path construction and <code>os.truncate()<\/code> for the actual resize operation when path-level truncation is the clearest fit.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/truncate-number-b217.png\" alt=\"Python Pool infographic comparing a number, int conversion, toward-zero result, and decimal\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>int truncates a floating value toward zero, which differs from floor for negatives.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_mathtrunc_for_Numbers\"><\/span>Use math.trunc() for Numbers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Numeric truncation is different from rounding. <code>math.trunc()<\/code> removes the decimal part and moves the number toward zero.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import math\n\nprint(math.trunc(3.9))\nprint(math.trunc(-3.9))\n<\/code><\/pre>\n<\/div>\n<p>The results are <code>3<\/code> and <code>-3<\/code>. Use rounding functions when you need nearest-value behavior, and use <code>math.trunc()<\/code> when you specifically want to discard the fractional part. This distinction matters for financial calculations, display formatting, and any logic where negative numbers are possible.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_Slicing_to_Truncate_Strings\"><\/span>Use Slicing to Truncate Strings<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Strings do not have a <code>truncate()<\/code> method. Use slicing to keep a prefix of a string or sequence. This creates a new string and leaves the original unchanged.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">text = \"PythonPool\"\n\nshort_text = text[:6]\n\nprint(short_text)\nprint(text)\n<\/code><\/pre>\n<\/div>\n<p>This prints <code>Python<\/code> and then the original value. If you are cleaning text before slicing, PythonPool&#8217;s <a href=\"https:\/\/www.pythonpool.com\/python-lowercase\/\">Python lowercase<\/a> guide covers related string cleanup patterns. Slicing is also the right approach for lists and tuples when you need a shortened copy instead of changing a file on disk.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Checklist\"><\/span>Checklist<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Use <code>file.truncate(size)<\/code> when you already have an open file object.<\/li>\n<li>Use <code>file.seek(position)<\/code> before <code>file.truncate()<\/code> when cutting at the cursor.<\/li>\n<li>Use <code>os.truncate(path, size)<\/code> when you want to resize by path.<\/li>\n<li>Use <code>math.trunc(number)<\/code> for numbers and slicing for strings.<\/li>\n<\/ul>\n<p>Before truncating files in production, make a backup or write to a temporary file first. Truncation removes data immediately, and the lost part is not recoverable from the file object after the operation. When in doubt, write a new file, verify it, and then replace the original.<\/p>\n<p>Also check whether you need truncation at all. Sometimes a safer workflow is to create a fresh output file from known input rather than editing an existing file in place.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/truncate-string-b217.png\" alt=\"Python Pool infographic showing text, length limit, slice, ellipsis, and shortened string\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>String truncation should define whether the limit includes an ellipsis or counts code points.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"References\"><\/span>References<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/io.html#io.IOBase.truncate\">Python documentation: IOBase.truncate()<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/os.html#os.truncate\">Python documentation: os.truncate()<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/math.html#math.trunc\">Python documentation: math.trunc()<\/a><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Truncate_An_Open_File_Deliberately\"><\/span>Truncate An Open File Deliberately<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>file.truncate(size) changes the file length and requires the file to be opened with appropriate permissions. Flush or close the file according to the workflow, and test a copy before modifying important data.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from tempfile import TemporaryFile\n\nwith TemporaryFile(mode=\"w+\") as handle:\n    handle.write(\"abcdef\")\n    handle.flush()\n    handle.truncate(3)\n    handle.seek(0)\n    print(handle.read())\n<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_ostruncate_For_A_Path\"><\/span>Use os.truncate For A Path<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>os.truncate works with a path or file descriptor when a path-level operation is more convenient. It still changes the underlying file, so validate the path and permissions before calling it.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import os\nfrom pathlib import Path\nfrom tempfile import TemporaryDirectory\n\nwith TemporaryDirectory() as folder:\n    path = Path(folder) \/ \"data.txt\"\n    path.write_text(\"abcdef\", encoding=\"utf-8\")\n    os.truncate(path, 3)\n    print(path.read_text(encoding=\"utf-8\"))\n<\/code><\/pre>\n<\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/truncate-list-b217.png\" alt=\"Python Pool infographic comparing a list, slice, del, length limit, and shortened list\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Choose slicing for a copy or del for an in-place list truncation.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Truncate_Numbers_Toward_Zero\"><\/span>Truncate Numbers Toward Zero<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>math.trunc removes the fractional part toward zero, which differs from floor for negative values. Use round when the business rule is rounding rather than truncation.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import math\n\nfor value in [3.9, -3.9]:\n    print(value, math.trunc(value), math.floor(value))\n<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Slice_Strings_And_Lists\"><\/span>Slice Strings And Lists<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sequences are not resized by a truncate method. Use a slice to create a shorter result, and remember that strings and list slices have different mutability and copy implications.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">text = \"Python Pool\"\nvalues = [1, 2, 3, 4]\n\nprint(text[:6])\nprint(values[:2])\n<\/code><\/pre>\n<\/div>\n<p>Use the official <a href=\"https:\/\/docs.python.org\/3\/library\/io.html#io.IOBase.truncate\">file truncate reference<\/a>, <a href=\"https:\/\/docs.python.org\/3\/library\/os.html#os.truncate\">os.truncate()<\/a>, and <a href=\"https:\/\/docs.python.org\/3\/library\/math.html#math.trunc\">math.trunc()<\/a>. Related guides include <a href=\"https:\/\/www.pythonpool.com\/python-trim\/\">string trim methods<\/a> and <a href=\"https:\/\/www.pythonpool.com\/python-round-down\/\">rounding down versus truncation<\/a>.<\/p>\n<p>For related size and rounding rules, compare <a href=\"https:\/\/www.pythonpool.com\/python-trim\/\">string trim methods<\/a>, <a href=\"https:\/\/www.pythonpool.com\/python-round-down\/\">rounding down<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/python-string-length\/\">string length<\/a> before choosing a file, number, or sequence operation.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_does_filetruncate_do\"><\/span>What does file.truncate() do?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It changes the length of an open file, removing data beyond the requested size or extending it with null bytes when the new size is larger.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_mathtrunc_used_for\"><\/span>What is math.trunc() used for?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>math.trunc() removes the fractional part of a real number by moving toward zero and returns an integer-like result.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_truncate_a_Python_string\"><\/span>How do I truncate a Python string?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use slicing such as text[:limit] to keep the desired prefix; strings are immutable, so slicing returns a new string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_filetruncate_and_ostruncate\"><\/span>What is the difference between file.truncate() and os.truncate()?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>file.truncate() works through an open file object, while os.truncate() changes the size of a path and requires the appropriate file permissions.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What does file.truncate() do?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"It changes the length of an open file, removing data beyond the requested size or extending it with null bytes when the new size is larger.\"}},{\"@type\":\"Question\",\"name\":\"What is math.trunc() used for?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"math.trunc() removes the fractional part of a real number by moving toward zero and returns an integer-like result.\"}},{\"@type\":\"Question\",\"name\":\"How do I truncate a Python string?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use slicing such as text[:limit] to keep the desired prefix; strings are immutable, so slicing returns a new string.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between file.truncate() and os.truncate()?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"file.truncate() works through an open file object, while os.truncate() changes the size of a path and requires the appropriate file permissions.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use Python truncate() for files, os.truncate() for paths, math.trunc() for numbers, and slicing for strings and lists.<\/p>\n","protected":false},"author":3,"featured_media":33044,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[296],"tags":[1356,1374,1362,1376,1373,1366,1368,1371,1354,1360,1353,1357,1364,1359,1367,1365,1372,1370,1375,1369,1363,1361,1355,1358],"class_list":["post-3496","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","tag-how-to-truncate-in-python","tag-python-file-truncate","tag-python-string-truncate","tag-python-truncate","tag-python-truncate-decimal","tag-python-truncate-file","tag-python-truncate-float","tag-python-truncate-float-to-2-decimal-places","tag-python-truncate-float-to-2-decimals","tag-python-truncate-float-to-int","tag-python-truncate-integer","tag-python-truncate-list","tag-python-truncate-number","tag-python-truncate-string","tag-truncate-a-float-python","tag-truncate-a-list-python","tag-truncate-decimal-python","tag-truncate-float-python","tag-truncate-in-python","tag-truncate-list-python","tag-truncate-number-python","tag-truncate-python-2","tag-truncate-string-python","tag-truncate-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Truncate: Files, Numbers, Strings, and Lists<\/title>\n<meta name=\"description\" content=\"Use Python truncate() for files, os.truncate() for paths, math.trunc() for numbers, and slicing for strings and lists.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-truncate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Truncate: Files, Numbers, Strings, and Lists\" \/>\n<meta property=\"og:description\" content=\"Use Python truncate() for files, os.truncate() for paths, math.trunc() for numbers, and slicing for strings and lists.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-truncate\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-09T11:00:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T06:57:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate.png\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwini Mandani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"Python Truncate: Files, Numbers, Strings, and Lists\",\"datePublished\":\"2020-07-09T11:00:42+00:00\",\"dateModified\":\"2026-07-13T06:57:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/\"},\"wordCount\":1109,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-truncate-files-values-guide-pythonpool.png\",\"keywords\":[\"how to truncate in python\",\"python file truncate\",\"python string truncate\",\"python truncate\",\"python truncate decimal\",\"python truncate file\",\"python truncate float\",\"python truncate float to 2 decimal places\",\"python truncate float to 2 decimals\",\"python truncate float to int\",\"python truncate integer\",\"python truncate list\",\"python truncate number\",\"python truncate string\",\"truncate a float python\",\"truncate a list python\",\"truncate decimal python\",\"truncate float python\",\"truncate in python\",\"truncate list python\",\"truncate number python\",\"truncate python\",\"truncate string python\",\"truncate() python\"],\"articleSection\":[\"How to\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/\",\"name\":\"Python Truncate: Files, Numbers, Strings, and Lists\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-truncate-files-values-guide-pythonpool.png\",\"datePublished\":\"2020-07-09T11:00:42+00:00\",\"dateModified\":\"2026-07-13T06:57:11+00:00\",\"description\":\"Use Python truncate() for files, os.truncate() for paths, math.trunc() for numbers, and slicing for strings and lists.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-truncate-files-values-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-truncate-files-values-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Python truncate guide showing file truncate os truncate math trunc and string slicing examples\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-truncate\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Truncate: Files, Numbers, Strings, and Lists\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Truncate: Files, Numbers, Strings, and Lists","description":"Use Python truncate() for files, os.truncate() for paths, math.trunc() for numbers, and slicing for strings and lists.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-truncate\/","og_locale":"en_US","og_type":"article","og_title":"Python Truncate: Files, Numbers, Strings, and Lists","og_description":"Use Python truncate() for files, os.truncate() for paths, math.trunc() for numbers, and slicing for strings and lists.","og_url":"https:\/\/www.pythonpool.com\/python-truncate\/","og_site_name":"Python Pool","article_published_time":"2020-07-09T11:00:42+00:00","article_modified_time":"2026-07-13T06:57:11+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate.png","type":"","width":"","height":""}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-truncate\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-truncate\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"Python Truncate: Files, Numbers, Strings, and Lists","datePublished":"2020-07-09T11:00:42+00:00","dateModified":"2026-07-13T06:57:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-truncate\/"},"wordCount":1109,"commentCount":6,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-truncate\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate-files-values-guide-pythonpool.png","keywords":["how to truncate in python","python file truncate","python string truncate","python truncate","python truncate decimal","python truncate file","python truncate float","python truncate float to 2 decimal places","python truncate float to 2 decimals","python truncate float to int","python truncate integer","python truncate list","python truncate number","python truncate string","truncate a float python","truncate a list python","truncate decimal python","truncate float python","truncate in python","truncate list python","truncate number python","truncate python","truncate string python","truncate() python"],"articleSection":["How to"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-truncate\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-truncate\/","url":"https:\/\/www.pythonpool.com\/python-truncate\/","name":"Python Truncate: Files, Numbers, Strings, and Lists","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-truncate\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-truncate\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate-files-values-guide-pythonpool.png","datePublished":"2020-07-09T11:00:42+00:00","dateModified":"2026-07-13T06:57:11+00:00","description":"Use Python truncate() for files, os.truncate() for paths, math.trunc() for numbers, and slicing for strings and lists.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-truncate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-truncate\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-truncate\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate-files-values-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-truncate-files-values-guide-pythonpool.png","width":1350,"height":650,"caption":"Python truncate guide showing file truncate os truncate math trunc and string slicing examples"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-truncate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python Truncate: Files, Numbers, Strings, and Lists"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3496"}],"version-history":[{"count":15,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3496\/revisions"}],"predecessor-version":[{"id":41381,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3496\/revisions\/41381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33044"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}