[{"data":1,"prerenderedAt":2748},["ShallowReactive",2],{"post-2025-07-09-python":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":14,"_type":2742,"_id":2743,"_source":2744,"_file":2745,"_stem":2746,"_extension":2747},"\u002Fblog\u002F2025-07-09-python","blog",false,"","Python-文件及目录操作","Python文件与目录操作：文件读写、with语句、os\u002Fpathlib模块常用功能。","2025-07-09",[12,13],"Python","基础",{"type":15,"children":16,"toc":2737},"root",[17,22,29,35,50,70,127,137,423,433,467,472,484,498,513,527,546,570,627,632,652,666,686,715,722,742,747,756,769,783,807,834,861,875,917,946,951,992,1000,1013,1027,1035,1047,1061,1066,1086,1098,1134,1144,1300,1310,1449,1454,1471,1494,1550,1573,1585,1607,1660,1680,1694,1713,1728,1733,1753,1775,1794,1798,1810,1834,1842,1862,1876,1907,1914,1926,1939,1966,1970,1989,2011,2030,2054,2059,2078,2100,2230,2243,2248,2258,2380,2395,2400,2419,2441,2452,2643,2648,2731],{"type":18,"tag":19,"props":20,"children":21},"element","hr",{},[],{"type":18,"tag":23,"props":24,"children":26},"h3",{"id":25},"基本文件操作",[27],{"type":28,"value":25},"text",{"type":18,"tag":30,"props":31,"children":33},"h4",{"id":32},"创建和打开文件",[34],{"type":28,"value":32},{"type":18,"tag":36,"props":37,"children":38},"p",{},[39,41,48],{"type":28,"value":40},"在Python中若要操作文件对象，需要先使用内置的 ",{"type":18,"tag":42,"props":43,"children":45},"code",{"className":44},[],[46],{"type":28,"value":47},"open()",{"type":28,"value":49}," 函数实现。其语法格式如下：",{"type":18,"tag":51,"props":52,"children":56},"pre",{"className":53,"code":54,"language":55,"meta":7,"style":7},"language-python shiki shiki-themes github-dark","file = open(filename[,mode[,buffering]])\n","python",[57],{"type":18,"tag":42,"props":58,"children":59},{"__ignoreMap":7},[60],{"type":18,"tag":61,"props":62,"children":65},"span",{"class":63,"line":64},"line",1,[66],{"type":18,"tag":61,"props":67,"children":68},{},[69],{"type":28,"value":54},{"type":18,"tag":71,"props":72,"children":73},"ul",{},[74,86,97,116],{"type":18,"tag":75,"props":76,"children":77},"li",{},[78,84],{"type":18,"tag":42,"props":79,"children":81},{"className":80},[],[82],{"type":28,"value":83},"file",{"type":28,"value":85},"：被创建的文件对象",{"type":18,"tag":75,"props":87,"children":88},{},[89,95],{"type":18,"tag":42,"props":90,"children":92},{"className":91},[],[93],{"type":28,"value":94},"filename",{"type":28,"value":96},"：要创建或者打开文件的名称，需要使用单引号或双引号括起来，若打开文件在当前目录下，则可以直接写文件名，否则需要指定完整路径",{"type":18,"tag":75,"props":98,"children":99},{},[100,106,108,114],{"type":18,"tag":42,"props":101,"children":103},{"className":102},[],[104],{"type":28,"value":105},"mode",{"type":28,"value":107},"：可选参数，用于指定文件的打开模式。默认为 ",{"type":18,"tag":42,"props":109,"children":111},{"className":110},[],[112],{"type":28,"value":113},"r",{"type":28,"value":115}," 即只读模式",{"type":18,"tag":75,"props":117,"children":118},{},[119,125],{"type":18,"tag":42,"props":120,"children":122},{"className":121},[],[123],{"type":28,"value":124},"buffering",{"type":28,"value":126},"：可选参数，用于指定读写文件的缓冲模式，若为0，则表示不缓存，若为1，则表示缓存；如大于1，则表示缓冲区的大小，默认为缓存模式",{"type":18,"tag":36,"props":128,"children":129},{},[130,135],{"type":18,"tag":42,"props":131,"children":133},{"className":132},[],[134],{"type":28,"value":105},{"type":28,"value":136}," 的参数值及其说明",{"type":18,"tag":138,"props":139,"children":140},"table",{},[141,166],{"type":18,"tag":142,"props":143,"children":144},"thead",{},[145],{"type":18,"tag":146,"props":147,"children":148},"tr",{},[149,156,161],{"type":18,"tag":150,"props":151,"children":153},"th",{"align":152},"center",[154],{"type":28,"value":155},"参数值",{"type":18,"tag":150,"props":157,"children":158},{"align":152},[159],{"type":28,"value":160},"说明",{"type":18,"tag":150,"props":162,"children":163},{"align":152},[164],{"type":28,"value":165},"文件必须存在",{"type":18,"tag":167,"props":168,"children":169},"tbody",{},[170,192,213,234,255,277,298,319,340,361,382,403],{"type":18,"tag":146,"props":171,"children":172},{},[173,182,187],{"type":18,"tag":174,"props":175,"children":176},"td",{"align":152},[177],{"type":18,"tag":42,"props":178,"children":180},{"className":179},[],[181],{"type":28,"value":113},{"type":18,"tag":174,"props":183,"children":184},{"align":152},[185],{"type":28,"value":186},"以只读模式打开文件，文件的指针将放置文件开头",{"type":18,"tag":174,"props":188,"children":189},{"align":152},[190],{"type":28,"value":191},"是",{"type":18,"tag":146,"props":193,"children":194},{},[195,204,209],{"type":18,"tag":174,"props":196,"children":197},{"align":152},[198],{"type":18,"tag":42,"props":199,"children":201},{"className":200},[],[202],{"type":28,"value":203},"rb",{"type":18,"tag":174,"props":205,"children":206},{"align":152},[207],{"type":28,"value":208},"以二进制格式打开文件，只读模式，指针放置开头，用于非文本文件",{"type":18,"tag":174,"props":210,"children":211},{"align":152},[212],{"type":28,"value":191},{"type":18,"tag":146,"props":214,"children":215},{},[216,225,230],{"type":18,"tag":174,"props":217,"children":218},{"align":152},[219],{"type":18,"tag":42,"props":220,"children":222},{"className":221},[],[223],{"type":28,"value":224},"r+",{"type":18,"tag":174,"props":226,"children":227},{"align":152},[228],{"type":28,"value":229},"打开文件后，可以读取文件内容，也可以写入新的内容覆盖掉原有内容（从头开始覆盖）",{"type":18,"tag":174,"props":231,"children":232},{"align":152},[233],{"type":28,"value":191},{"type":18,"tag":146,"props":235,"children":236},{},[237,246,251],{"type":18,"tag":174,"props":238,"children":239},{"align":152},[240],{"type":18,"tag":42,"props":241,"children":243},{"className":242},[],[244],{"type":28,"value":245},"rb+",{"type":18,"tag":174,"props":247,"children":248},{"align":152},[249],{"type":28,"value":250},"以二进制格式打开文件，读写模式，指针放置开头，用于非文本文件",{"type":18,"tag":174,"props":252,"children":253},{"align":152},[254],{"type":28,"value":191},{"type":18,"tag":146,"props":256,"children":257},{},[258,267,272],{"type":18,"tag":174,"props":259,"children":260},{"align":152},[261],{"type":18,"tag":42,"props":262,"children":264},{"className":263},[],[265],{"type":28,"value":266},"w",{"type":18,"tag":174,"props":268,"children":269},{"align":152},[270],{"type":28,"value":271},"以只写模式打开文件",{"type":18,"tag":174,"props":273,"children":274},{"align":152},[275],{"type":28,"value":276},"否",{"type":18,"tag":146,"props":278,"children":279},{},[280,289,294],{"type":18,"tag":174,"props":281,"children":282},{"align":152},[283],{"type":18,"tag":42,"props":284,"children":286},{"className":285},[],[287],{"type":28,"value":288},"wb",{"type":18,"tag":174,"props":290,"children":291},{"align":152},[292],{"type":28,"value":293},"以二进制格式打开文件，只写模式，用于非文本文件",{"type":18,"tag":174,"props":295,"children":296},{"align":152},[297],{"type":28,"value":276},{"type":18,"tag":146,"props":299,"children":300},{},[301,310,315],{"type":18,"tag":174,"props":302,"children":303},{"align":152},[304],{"type":18,"tag":42,"props":305,"children":307},{"className":306},[],[308],{"type":28,"value":309},"w+",{"type":18,"tag":174,"props":311,"children":312},{"align":152},[313],{"type":28,"value":314},"打开文件后，先清空内容，对空文件有读写权限",{"type":18,"tag":174,"props":316,"children":317},{"align":152},[318],{"type":28,"value":276},{"type":18,"tag":146,"props":320,"children":321},{},[322,331,336],{"type":18,"tag":174,"props":323,"children":324},{"align":152},[325],{"type":18,"tag":42,"props":326,"children":328},{"className":327},[],[329],{"type":28,"value":330},"wb+",{"type":18,"tag":174,"props":332,"children":333},{"align":152},[334],{"type":28,"value":335},"以二进制格式打开文件，读写模式，用于非文本文件",{"type":18,"tag":174,"props":337,"children":338},{"align":152},[339],{"type":28,"value":276},{"type":18,"tag":146,"props":341,"children":342},{},[343,352,357],{"type":18,"tag":174,"props":344,"children":345},{"align":152},[346],{"type":18,"tag":42,"props":347,"children":349},{"className":348},[],[350],{"type":28,"value":351},"a",{"type":18,"tag":174,"props":353,"children":354},{"align":152},[355],{"type":28,"value":356},"以追加模式打开文件，若文件存在，文件指针放置于末尾，否则",{"type":18,"tag":174,"props":358,"children":359},{"align":152},[360],{"type":28,"value":276},{"type":18,"tag":146,"props":362,"children":363},{},[364,373,378],{"type":18,"tag":174,"props":365,"children":366},{"align":152},[367],{"type":18,"tag":42,"props":368,"children":370},{"className":369},[],[371],{"type":28,"value":372},"ab",{"type":18,"tag":174,"props":374,"children":375},{"align":152},[376],{"type":28,"value":377},"二进制格式打开文件，追加模式，指针放置末尾",{"type":18,"tag":174,"props":379,"children":380},{"align":152},[381],{"type":28,"value":276},{"type":18,"tag":146,"props":383,"children":384},{},[385,394,399],{"type":18,"tag":174,"props":386,"children":387},{"align":152},[388],{"type":18,"tag":42,"props":389,"children":391},{"className":390},[],[392],{"type":28,"value":393},"a+",{"type":18,"tag":174,"props":395,"children":396},{"align":152},[397],{"type":28,"value":398},"以读写模式打开文件，指针放置末尾",{"type":18,"tag":174,"props":400,"children":401},{"align":152},[402],{"type":28,"value":276},{"type":18,"tag":146,"props":404,"children":405},{},[406,415,419],{"type":18,"tag":174,"props":407,"children":408},{"align":152},[409],{"type":18,"tag":42,"props":410,"children":412},{"className":411},[],[413],{"type":28,"value":414},"ab+",{"type":18,"tag":174,"props":416,"children":417},{"align":152},[418],{"type":28,"value":377},{"type":18,"tag":174,"props":420,"children":421},{"align":152},[422],{"type":28,"value":276},{"type":18,"tag":36,"props":424,"children":425},{},[426,431],{"type":18,"tag":42,"props":427,"children":429},{"className":428},[],[430],{"type":28,"value":47},{"type":28,"value":432},"方法经常实现以下几个功能：",{"type":18,"tag":71,"props":434,"children":435},{},[436,441,446],{"type":18,"tag":75,"props":437,"children":438},{},[439],{"type":28,"value":440},"打开一个不存在的文件时先创建该文件",{"type":18,"tag":75,"props":442,"children":443},{},[444],{"type":28,"value":445},"以二进制形式打开文件，如图片、音视频文件等",{"type":18,"tag":75,"props":447,"children":448},{},[449,451,457,459,465],{"type":28,"value":450},"打开文件时指定编码方式，默认",{"type":18,"tag":42,"props":452,"children":454},{"className":453},[],[455],{"type":28,"value":456},"GBK",{"type":28,"value":458},"编码格式，若打开文件与编码不符将会抛出",{"type":18,"tag":42,"props":460,"children":462},{"className":461},[],[463],{"type":28,"value":464},"UnicodeDecodeError",{"type":28,"value":466},"异常信息",{"type":18,"tag":30,"props":468,"children":470},{"id":469},"关闭文件",[471],{"type":28,"value":469},{"type":18,"tag":36,"props":473,"children":474},{},[475,477,482],{"type":28,"value":476},"打开文件后，需要及时关闭文件，以免对文件造成破坏。如关闭文件名为",{"type":18,"tag":42,"props":478,"children":480},{"className":479},[],[481],{"type":28,"value":83},{"type":28,"value":483},"的文件语法如下：",{"type":18,"tag":51,"props":485,"children":487},{"className":53,"code":486,"language":55,"meta":7,"style":7},"file.close()\n",[488],{"type":18,"tag":42,"props":489,"children":490},{"__ignoreMap":7},[491],{"type":18,"tag":61,"props":492,"children":493},{"class":63,"line":64},[494],{"type":18,"tag":61,"props":495,"children":496},{},[497],{"type":28,"value":486},{"type":18,"tag":499,"props":500,"children":501},"blockquote",{},[502],{"type":18,"tag":36,"props":503,"children":504},{},[505,511],{"type":18,"tag":42,"props":506,"children":508},{"className":507},[],[509],{"type":28,"value":510},"close()",{"type":28,"value":512},"方法先刷新缓冲区中还未写入的信息，然后关闭文件",{"type":18,"tag":30,"props":514,"children":516},{"id":515},"打开文件使用with语句",[517,519,525],{"type":28,"value":518},"打开文件使用",{"type":18,"tag":42,"props":520,"children":522},{"className":521},[],[523],{"type":28,"value":524},"with",{"type":28,"value":526},"语句",{"type":18,"tag":36,"props":528,"children":529},{},[530,532,537,539,544],{"type":28,"value":531},"使用",{"type":18,"tag":42,"props":533,"children":535},{"className":534},[],[536],{"type":28,"value":524},{"type":28,"value":538},"语句可以实现无论是否抛出异常都将保证",{"type":18,"tag":42,"props":540,"children":542},{"className":541},[],[543],{"type":28,"value":524},{"type":28,"value":545},"语句执行完毕后关闭已经打开的文件，其语法格式如下：",{"type":18,"tag":51,"props":547,"children":549},{"className":53,"code":548,"language":55,"meta":7,"style":7},"with expression as target:\n    with_body\n",[550],{"type":18,"tag":42,"props":551,"children":552},{"__ignoreMap":7},[553,561],{"type":18,"tag":61,"props":554,"children":555},{"class":63,"line":64},[556],{"type":18,"tag":61,"props":557,"children":558},{},[559],{"type":28,"value":560},"with expression as target:\n",{"type":18,"tag":61,"props":562,"children":564},{"class":63,"line":563},2,[565],{"type":18,"tag":61,"props":566,"children":567},{},[568],{"type":28,"value":569},"    with_body\n",{"type":18,"tag":71,"props":571,"children":572},{},[573,591,609],{"type":18,"tag":75,"props":574,"children":575},{},[576,582,584,589],{"type":18,"tag":42,"props":577,"children":579},{"className":578},[],[580],{"type":28,"value":581},"expression",{"type":28,"value":583},"：用于指定一个表达式，这里可以是",{"type":18,"tag":42,"props":585,"children":587},{"className":586},[],[588],{"type":28,"value":47},{"type":28,"value":590},"函数",{"type":18,"tag":75,"props":592,"children":593},{},[594,600,602,607],{"type":18,"tag":42,"props":595,"children":597},{"className":596},[],[598],{"type":28,"value":599},"target",{"type":28,"value":601},"：指定一个变量，并将",{"type":18,"tag":42,"props":603,"children":605},{"className":604},[],[606],{"type":28,"value":581},{"type":28,"value":608},"的结果保存在该变量中",{"type":18,"tag":75,"props":610,"children":611},{},[612,618,620,625],{"type":18,"tag":42,"props":613,"children":615},{"className":614},[],[616],{"type":28,"value":617},"with_body",{"type":28,"value":619},"：用于指定",{"type":18,"tag":42,"props":621,"children":623},{"className":622},[],[624],{"type":28,"value":524},{"type":28,"value":626},"语句体",{"type":18,"tag":30,"props":628,"children":630},{"id":629},"写入文件内容",[631],{"type":28,"value":629},{"type":18,"tag":36,"props":633,"children":634},{},[635,637,643,645,650],{"type":28,"value":636},"Python的文件对象提供了",{"type":18,"tag":42,"props":638,"children":640},{"className":639},[],[641],{"type":28,"value":642},"write()",{"type":28,"value":644},"方法，可以向文件写入内容，",{"type":18,"tag":42,"props":646,"children":648},{"className":647},[],[649],{"type":28,"value":642},{"type":28,"value":651},"方法如下：",{"type":18,"tag":51,"props":653,"children":655},{"className":53,"code":654,"language":55,"meta":7,"style":7},"file.write(string)\n",[656],{"type":18,"tag":42,"props":657,"children":658},{"__ignoreMap":7},[659],{"type":18,"tag":61,"props":660,"children":661},{"class":63,"line":64},[662],{"type":18,"tag":61,"props":663,"children":664},{},[665],{"type":28,"value":654},{"type":18,"tag":36,"props":667,"children":668},{},[669,671,676,678,684],{"type":28,"value":670},"其中",{"type":18,"tag":42,"props":672,"children":674},{"className":673},[],[675],{"type":28,"value":83},{"type":28,"value":677},"为文件对象，",{"type":18,"tag":42,"props":679,"children":681},{"className":680},[],[682],{"type":28,"value":683},"string",{"type":28,"value":685},"为需要写入的字符串。",{"type":18,"tag":499,"props":687,"children":688},{},[689],{"type":18,"tag":36,"props":690,"children":691},{},[692,694,699,701,706,708,713],{"type":28,"value":693},"调用",{"type":18,"tag":42,"props":695,"children":697},{"className":696},[],[698],{"type":28,"value":642},{"type":28,"value":700},"方法向文件写入内容的前提是，在打开文件时，指定的打开模式为",{"type":18,"tag":42,"props":702,"children":704},{"className":703},[],[705],{"type":28,"value":266},{"type":28,"value":707},"或者",{"type":18,"tag":42,"props":709,"children":711},{"className":710},[],[712],{"type":28,"value":351},{"type":28,"value":714},"，否则将会抛出异常（没有写入权限）。",{"type":18,"tag":716,"props":717,"children":719},"font",{"color":718},"red",[720],{"type":28,"value":721},"\n注意：在写入文件后一定要调用close()方法关闭文件，否则，写入的内容不会被保存到文件中。这是因为，当我们在写入文件内容时，操作系统不会立即把数据写入磁盘中，而是先缓存起来，只有当调用close()方法时，操作系统才会保证把没有写入的数据全部写入磁盘中。另外，在向文件写入内容后，如果不想马上关闭文件，也可以调用文件对象提供的flush()方法，把缓冲区的内容写入文件中，这样也能保证将全部信息写入文件。\n",{"type":18,"tag":36,"props":723,"children":724},{},[725,727,732,734,740],{"type":28,"value":726},"除了",{"type":18,"tag":42,"props":728,"children":730},{"className":729},[],[731],{"type":28,"value":642},{"type":28,"value":733},"方法外，Python还提供了另一种写入方法，即",{"type":18,"tag":42,"props":735,"children":737},{"className":736},[],[738],{"type":28,"value":739},"writelines()",{"type":28,"value":741},"方法，可以实现把字符串列表写入文件，但是不添加换行符。",{"type":18,"tag":30,"props":743,"children":745},{"id":744},"读取文件",[746],{"type":28,"value":744},{"type":18,"tag":36,"props":748,"children":749},{},[750],{"type":18,"tag":751,"props":752,"children":753},"strong",{},[754],{"type":28,"value":755},"读取指定字符",{"type":18,"tag":36,"props":757,"children":758},{},[759,761,767],{"type":28,"value":760},"文件对象体提供了",{"type":18,"tag":42,"props":762,"children":764},{"className":763},[],[765],{"type":28,"value":766},"read()",{"type":28,"value":768},"方法读取指定个数的字符。其语法格式如下：",{"type":18,"tag":51,"props":770,"children":772},{"className":53,"code":771,"language":55,"meta":7,"style":7},"file.read([size])\n",[773],{"type":18,"tag":42,"props":774,"children":775},{"__ignoreMap":7},[776],{"type":18,"tag":61,"props":777,"children":778},{"class":63,"line":64},[779],{"type":18,"tag":61,"props":780,"children":781},{},[782],{"type":28,"value":771},{"type":18,"tag":71,"props":784,"children":785},{},[786,796],{"type":18,"tag":75,"props":787,"children":788},{},[789,794],{"type":18,"tag":42,"props":790,"children":792},{"className":791},[],[793],{"type":28,"value":83},{"type":28,"value":795},"：打开的文件对象",{"type":18,"tag":75,"props":797,"children":798},{},[799,805],{"type":18,"tag":42,"props":800,"children":802},{"className":801},[],[803],{"type":28,"value":804},"size",{"type":28,"value":806},"：可选参数，用于指定要读取的字符个数，若省略，则一次性读取所有内容",{"type":18,"tag":499,"props":808,"children":809},{},[810],{"type":18,"tag":36,"props":811,"children":812},{},[813,814,819,821,826,827,832],{"type":28,"value":693},{"type":18,"tag":42,"props":815,"children":817},{"className":816},[],[818],{"type":28,"value":766},{"type":28,"value":820},"方法读取文件内容的前提是，打开文件时，指定的打开模式为",{"type":18,"tag":42,"props":822,"children":824},{"className":823},[],[825],{"type":28,"value":113},{"type":28,"value":707},{"type":18,"tag":42,"props":828,"children":830},{"className":829},[],[831],{"type":28,"value":224},{"type":28,"value":833},"，否则，将抛出异常信息。",{"type":18,"tag":36,"props":835,"children":836},{},[837,838,844,846,852,854,859],{"type":28,"value":531},{"type":18,"tag":42,"props":839,"children":841},{"className":840},[],[842],{"type":28,"value":843},"read(size)",{"type":28,"value":845},"方法读取文件时，是从文件的开头读取的。若要读取部分内容，可以先使用文件对象的",{"type":18,"tag":42,"props":847,"children":849},{"className":848},[],[850],{"type":28,"value":851},"seek()",{"type":28,"value":853},"方法将文件的指针移动到新的位置处，然后使用",{"type":18,"tag":42,"props":855,"children":857},{"className":856},[],[858],{"type":28,"value":843},{"type":28,"value":860},"方法进行读取，其语法格式如下：",{"type":18,"tag":51,"props":862,"children":864},{"className":53,"code":863,"language":55,"meta":7,"style":7},"file.seek(offset[,whence])\n",[865],{"type":18,"tag":42,"props":866,"children":867},{"__ignoreMap":7},[868],{"type":18,"tag":61,"props":869,"children":870},{"class":63,"line":64},[871],{"type":18,"tag":61,"props":872,"children":873},{},[874],{"type":28,"value":863},{"type":18,"tag":71,"props":876,"children":877},{},[878,888,907],{"type":18,"tag":75,"props":879,"children":880},{},[881,886],{"type":18,"tag":42,"props":882,"children":884},{"className":883},[],[885],{"type":28,"value":83},{"type":28,"value":887},"：表示已打开的文件对象",{"type":18,"tag":75,"props":889,"children":890},{},[891,897,899,905],{"type":18,"tag":42,"props":892,"children":894},{"className":893},[],[895],{"type":28,"value":896},"offset",{"type":28,"value":898},"：用于指定移动的字符个数，其具体位置与",{"type":18,"tag":42,"props":900,"children":902},{"className":901},[],[903],{"type":28,"value":904},"whence",{"type":28,"value":906},"相关",{"type":18,"tag":75,"props":908,"children":909},{},[910,915],{"type":18,"tag":42,"props":911,"children":913},{"className":912},[],[914],{"type":28,"value":904},{"type":28,"value":916},"：用于指定从什么位置开始计算，如果值为0，则表示从头开始计算；如果值为1，则表示从当前位置开始计算，如果值为2，则表示从文件尾开始计算。默认为0.",{"type":18,"tag":499,"props":918,"children":919},{},[920],{"type":18,"tag":36,"props":921,"children":922},{},[923,925,930,932,937,939,944],{"type":28,"value":924},"对于",{"type":18,"tag":42,"props":926,"children":928},{"className":927},[],[929],{"type":28,"value":904},{"type":28,"value":931},"参数，如果在打开文件时未使用b模式，则只允许从文件头开始计算相对位置。",{"type":18,"tag":42,"props":933,"children":935},{"className":934},[],[936],{"type":28,"value":896},{"type":28,"value":938},"的值按一个汉字占两个字符、英文和数字都占一个字符计算的，这与",{"type":18,"tag":42,"props":940,"children":942},{"className":941},[],[943],{"type":28,"value":843},{"type":28,"value":945},"方法不同。",{"type":18,"tag":36,"props":947,"children":948},{},[949],{"type":28,"value":950},"例如：若要读取从第10个字符开始读取5个字符，示例代码如下：",{"type":18,"tag":51,"props":952,"children":954},{"className":53,"code":953,"language":55,"meta":7,"style":7},"with open('message.txt','r') as file:\n    file.seek(10)\n    string = file.read(5)\n    print(string)\n",[955],{"type":18,"tag":42,"props":956,"children":957},{"__ignoreMap":7},[958,966,974,983],{"type":18,"tag":61,"props":959,"children":960},{"class":63,"line":64},[961],{"type":18,"tag":61,"props":962,"children":963},{},[964],{"type":28,"value":965},"with open('message.txt','r') as file:\n",{"type":18,"tag":61,"props":967,"children":968},{"class":63,"line":563},[969],{"type":18,"tag":61,"props":970,"children":971},{},[972],{"type":28,"value":973},"    file.seek(10)\n",{"type":18,"tag":61,"props":975,"children":977},{"class":63,"line":976},3,[978],{"type":18,"tag":61,"props":979,"children":980},{},[981],{"type":28,"value":982},"    string = file.read(5)\n",{"type":18,"tag":61,"props":984,"children":986},{"class":63,"line":985},4,[987],{"type":18,"tag":61,"props":988,"children":989},{},[990],{"type":28,"value":991},"    print(string)\n",{"type":18,"tag":36,"props":993,"children":994},{},[995],{"type":18,"tag":751,"props":996,"children":997},{},[998],{"type":28,"value":999},"读取一行",{"type":18,"tag":36,"props":1001,"children":1002},{},[1003,1005,1011],{"type":28,"value":1004},"文件对象提供了",{"type":18,"tag":42,"props":1006,"children":1008},{"className":1007},[],[1009],{"type":28,"value":1010},"readline()",{"type":28,"value":1012},"方法用于每次读取一行数据，其语法格式如下：",{"type":18,"tag":51,"props":1014,"children":1016},{"className":53,"code":1015,"language":55,"meta":7,"style":7},"file.readline()\n",[1017],{"type":18,"tag":42,"props":1018,"children":1019},{"__ignoreMap":7},[1020],{"type":18,"tag":61,"props":1021,"children":1022},{"class":63,"line":64},[1023],{"type":18,"tag":61,"props":1024,"children":1025},{},[1026],{"type":28,"value":1015},{"type":18,"tag":36,"props":1028,"children":1029},{},[1030],{"type":18,"tag":751,"props":1031,"children":1032},{},[1033],{"type":28,"value":1034},"读取全部行",{"type":18,"tag":36,"props":1036,"children":1037},{},[1038,1039,1045],{"type":28,"value":1004},{"type":18,"tag":42,"props":1040,"children":1042},{"className":1041},[],[1043],{"type":28,"value":1044},"readlines()",{"type":28,"value":1046},"方法用于获取全部行，返回值为一个字符串列表，其语法格式如下：",{"type":18,"tag":51,"props":1048,"children":1050},{"className":53,"code":1049,"language":55,"meta":7,"style":7},"file.readlines()\n",[1051],{"type":18,"tag":42,"props":1052,"children":1053},{"__ignoreMap":7},[1054],{"type":18,"tag":61,"props":1055,"children":1056},{"class":63,"line":64},[1057],{"type":18,"tag":61,"props":1058,"children":1059},{},[1060],{"type":28,"value":1049},{"type":18,"tag":23,"props":1062,"children":1064},{"id":1063},"目录操作",[1065],{"type":28,"value":1063},{"type":18,"tag":30,"props":1067,"children":1069},{"id":1068},"os-和-ospath-模块",[1070,1076,1078,1084],{"type":18,"tag":42,"props":1071,"children":1073},{"className":1072},[],[1074],{"type":28,"value":1075},"os",{"type":28,"value":1077}," 和 ",{"type":18,"tag":42,"props":1079,"children":1081},{"className":1080},[],[1082],{"type":28,"value":1083},"os.path",{"type":28,"value":1085}," 模块",{"type":18,"tag":36,"props":1087,"children":1088},{},[1089,1091,1096],{"type":28,"value":1090},"通过 ",{"type":18,"tag":42,"props":1092,"children":1094},{"className":1093},[],[1095],{"type":28,"value":1075},{"type":28,"value":1097}," 模块提供的通用变量，可获取与系统有关的信息。常用的变量有以下几个：",{"type":18,"tag":71,"props":1099,"children":1100},{},[1101,1112,1123],{"type":18,"tag":75,"props":1102,"children":1103},{},[1104,1110],{"type":18,"tag":42,"props":1105,"children":1107},{"className":1106},[],[1108],{"type":28,"value":1109},"name",{"type":28,"value":1111},"：用于获取操作系统",{"type":18,"tag":75,"props":1113,"children":1114},{},[1115,1121],{"type":18,"tag":42,"props":1116,"children":1118},{"className":1117},[],[1119],{"type":28,"value":1120},"linesep",{"type":28,"value":1122},"：获取当前操作系统上的换行符",{"type":18,"tag":75,"props":1124,"children":1125},{},[1126,1132],{"type":18,"tag":42,"props":1127,"children":1129},{"className":1128},[],[1130],{"type":28,"value":1131},"sep",{"type":28,"value":1133},"：用于获取当前操作系统使用的路径分隔符",{"type":18,"tag":36,"props":1135,"children":1136},{},[1137,1142],{"type":18,"tag":42,"props":1138,"children":1140},{"className":1139},[],[1141],{"type":28,"value":1075},{"type":28,"value":1143}," 模块还提供了一些操作目录的函数，如：",{"type":18,"tag":138,"props":1145,"children":1146},{},[1147,1161],{"type":18,"tag":142,"props":1148,"children":1149},{},[1150],{"type":18,"tag":146,"props":1151,"children":1152},{},[1153,1157],{"type":18,"tag":150,"props":1154,"children":1155},{"align":152},[1156],{"type":28,"value":590},{"type":18,"tag":150,"props":1158,"children":1159},{"align":152},[1160],{"type":28,"value":160},{"type":18,"tag":167,"props":1162,"children":1163},{},[1164,1181,1198,1215,1232,1249,1266,1283],{"type":18,"tag":146,"props":1165,"children":1166},{},[1167,1176],{"type":18,"tag":174,"props":1168,"children":1169},{"align":152},[1170],{"type":18,"tag":42,"props":1171,"children":1173},{"className":1172},[],[1174],{"type":28,"value":1175},"getcwd()",{"type":18,"tag":174,"props":1177,"children":1178},{"align":152},[1179],{"type":28,"value":1180},"返回当前的工作目录",{"type":18,"tag":146,"props":1182,"children":1183},{},[1184,1193],{"type":18,"tag":174,"props":1185,"children":1186},{"align":152},[1187],{"type":18,"tag":42,"props":1188,"children":1190},{"className":1189},[],[1191],{"type":28,"value":1192},"listdir(path)",{"type":18,"tag":174,"props":1194,"children":1195},{"align":152},[1196],{"type":28,"value":1197},"返回指定路径下的文件和目录信息",{"type":18,"tag":146,"props":1199,"children":1200},{},[1201,1210],{"type":18,"tag":174,"props":1202,"children":1203},{"align":152},[1204],{"type":18,"tag":42,"props":1205,"children":1207},{"className":1206},[],[1208],{"type":28,"value":1209},"mkdir(path[,mode])",{"type":18,"tag":174,"props":1211,"children":1212},{"align":152},[1213],{"type":28,"value":1214},"创建目录",{"type":18,"tag":146,"props":1216,"children":1217},{},[1218,1227],{"type":18,"tag":174,"props":1219,"children":1220},{"align":152},[1221],{"type":18,"tag":42,"props":1222,"children":1224},{"className":1223},[],[1225],{"type":28,"value":1226},"makedirs(path1\u002Fpath2...[,mode])",{"type":18,"tag":174,"props":1228,"children":1229},{"align":152},[1230],{"type":28,"value":1231},"创建多级目录",{"type":18,"tag":146,"props":1233,"children":1234},{},[1235,1244],{"type":18,"tag":174,"props":1236,"children":1237},{"align":152},[1238],{"type":18,"tag":42,"props":1239,"children":1241},{"className":1240},[],[1242],{"type":28,"value":1243},"rmdir(path)",{"type":18,"tag":174,"props":1245,"children":1246},{"align":152},[1247],{"type":28,"value":1248},"删除目录",{"type":18,"tag":146,"props":1250,"children":1251},{},[1252,1261],{"type":18,"tag":174,"props":1253,"children":1254},{"align":152},[1255],{"type":18,"tag":42,"props":1256,"children":1258},{"className":1257},[],[1259],{"type":28,"value":1260},"removedirs(path1\u002Fpath2)",{"type":18,"tag":174,"props":1262,"children":1263},{"align":152},[1264],{"type":28,"value":1265},"删除多级目录",{"type":18,"tag":146,"props":1267,"children":1268},{},[1269,1278],{"type":18,"tag":174,"props":1270,"children":1271},{"align":152},[1272],{"type":18,"tag":42,"props":1273,"children":1275},{"className":1274},[],[1276],{"type":28,"value":1277},"chdir(path)",{"type":18,"tag":174,"props":1279,"children":1280},{"align":152},[1281],{"type":28,"value":1282},"把path设置为当前工作目录",{"type":18,"tag":146,"props":1284,"children":1285},{},[1286,1295],{"type":18,"tag":174,"props":1287,"children":1288},{"align":152},[1289],{"type":18,"tag":42,"props":1290,"children":1292},{"className":1291},[],[1293],{"type":28,"value":1294},"walk(top[,topdown[,onerror]])",{"type":18,"tag":174,"props":1296,"children":1297},{"align":152},[1298],{"type":28,"value":1299},"遍历目录树，该方法返回一个元组，包括所有路径名、所有目录列表和文件列表3个元素",{"type":18,"tag":36,"props":1301,"children":1302},{},[1303,1308],{"type":18,"tag":42,"props":1304,"children":1306},{"className":1305},[],[1307],{"type":28,"value":1083},{"type":28,"value":1309}," 模块也提供了一些操作目录的函数，如：",{"type":18,"tag":138,"props":1311,"children":1312},{},[1313,1327],{"type":18,"tag":142,"props":1314,"children":1315},{},[1316],{"type":18,"tag":146,"props":1317,"children":1318},{},[1319,1323],{"type":18,"tag":150,"props":1320,"children":1321},{"align":152},[1322],{"type":28,"value":590},{"type":18,"tag":150,"props":1324,"children":1325},{"align":152},[1326],{"type":28,"value":160},{"type":18,"tag":167,"props":1328,"children":1329},{},[1330,1347,1364,1381,1398,1415,1432],{"type":18,"tag":146,"props":1331,"children":1332},{},[1333,1342],{"type":18,"tag":174,"props":1334,"children":1335},{"align":152},[1336],{"type":18,"tag":42,"props":1337,"children":1339},{"className":1338},[],[1340],{"type":28,"value":1341},"abspath(path)",{"type":18,"tag":174,"props":1343,"children":1344},{"align":152},[1345],{"type":28,"value":1346},"用于获取文件或目录的绝对路径",{"type":18,"tag":146,"props":1348,"children":1349},{},[1350,1359],{"type":18,"tag":174,"props":1351,"children":1352},{"align":152},[1353],{"type":18,"tag":42,"props":1354,"children":1356},{"className":1355},[],[1357],{"type":28,"value":1358},"exists(path)",{"type":18,"tag":174,"props":1360,"children":1361},{"align":152},[1362],{"type":28,"value":1363},"用于判断目录或者文件是否存在，bool型",{"type":18,"tag":146,"props":1365,"children":1366},{},[1367,1376],{"type":18,"tag":174,"props":1368,"children":1369},{"align":152},[1370],{"type":18,"tag":42,"props":1371,"children":1373},{"className":1372},[],[1374],{"type":28,"value":1375},"join(path,name)",{"type":18,"tag":174,"props":1377,"children":1378},{"align":152},[1379],{"type":28,"value":1380},"将目录与目录或者文件名拼接起来",{"type":18,"tag":146,"props":1382,"children":1383},{},[1384,1393],{"type":18,"tag":174,"props":1385,"children":1386},{"align":152},[1387],{"type":18,"tag":42,"props":1388,"children":1390},{"className":1389},[],[1391],{"type":28,"value":1392},"splitext()",{"type":18,"tag":174,"props":1394,"children":1395},{"align":152},[1396],{"type":28,"value":1397},"分离文件名和扩展名",{"type":18,"tag":146,"props":1399,"children":1400},{},[1401,1410],{"type":18,"tag":174,"props":1402,"children":1403},{"align":152},[1404],{"type":18,"tag":42,"props":1405,"children":1407},{"className":1406},[],[1408],{"type":28,"value":1409},"basename(path)",{"type":18,"tag":174,"props":1411,"children":1412},{"align":152},[1413],{"type":28,"value":1414},"从一个目录中提取文件名",{"type":18,"tag":146,"props":1416,"children":1417},{},[1418,1427],{"type":18,"tag":174,"props":1419,"children":1420},{"align":152},[1421],{"type":18,"tag":42,"props":1422,"children":1424},{"className":1423},[],[1425],{"type":28,"value":1426},"dirname(path)",{"type":18,"tag":174,"props":1428,"children":1429},{"align":152},[1430],{"type":28,"value":1431},"从一个路径中提取文件路径，不包括文件名",{"type":18,"tag":146,"props":1433,"children":1434},{},[1435,1444],{"type":18,"tag":174,"props":1436,"children":1437},{"align":152},[1438],{"type":18,"tag":42,"props":1439,"children":1441},{"className":1440},[],[1442],{"type":28,"value":1443},"isdir(path)",{"type":18,"tag":174,"props":1445,"children":1446},{"align":152},[1447],{"type":28,"value":1448},"用于判断是否为有效路径",{"type":18,"tag":30,"props":1450,"children":1452},{"id":1451},"路径",[1453],{"type":28,"value":1451},{"type":18,"tag":36,"props":1455,"children":1456},{},[1457,1462,1464,1469],{"type":18,"tag":751,"props":1458,"children":1459},{},[1460],{"type":28,"value":1461},"当前工作目录",{"type":28,"value":1463},"是指当前文件所在目录的目录。在Python中通过",{"type":18,"tag":42,"props":1465,"children":1467},{"className":1466},[],[1468],{"type":28,"value":1175},{"type":28,"value":1470},"函数获取当前工作目录。如：",{"type":18,"tag":51,"props":1472,"children":1474},{"className":53,"code":1473,"language":55,"meta":7,"style":7},"import os\nprint(os.getcwd())\n",[1475],{"type":18,"tag":42,"props":1476,"children":1477},{"__ignoreMap":7},[1478,1486],{"type":18,"tag":61,"props":1479,"children":1480},{"class":63,"line":64},[1481],{"type":18,"tag":61,"props":1482,"children":1483},{},[1484],{"type":28,"value":1485},"import os\n",{"type":18,"tag":61,"props":1487,"children":1488},{"class":63,"line":563},[1489],{"type":18,"tag":61,"props":1490,"children":1491},{},[1492],{"type":28,"value":1493},"print(os.getcwd())\n",{"type":18,"tag":36,"props":1495,"children":1496},{},[1497,1502,1504,1510,1512,1518,1520,1526,1528,1534,1536,1541,1543,1548],{"type":18,"tag":751,"props":1498,"children":1499},{},[1500],{"type":28,"value":1501},"相对路径",{"type":28,"value":1503},"依赖于当前工作目录，即在当前目录下的其他文件或子路径下的文件都可以通过",{"type":18,"tag":42,"props":1505,"children":1507},{"className":1506},[],[1508],{"type":28,"value":1509},"[子路径名\u002F...]\u002F文件",{"type":28,"value":1511},"方式访问。假设在",{"type":18,"tag":42,"props":1513,"children":1515},{"className":1514},[],[1516],{"type":28,"value":1517},"D:file\\code",{"type":28,"value":1519},"目录下存在",{"type":18,"tag":42,"props":1521,"children":1523},{"className":1522},[],[1524],{"type":28,"value":1525},"info",{"type":28,"value":1527},"子目录，子目录下存在",{"type":18,"tag":42,"props":1529,"children":1531},{"className":1530},[],[1532],{"type":28,"value":1533},"demo.txt",{"type":28,"value":1535},"文件。现在要使用",{"type":18,"tag":42,"props":1537,"children":1539},{"className":1538},[],[1540],{"type":28,"value":47},{"type":28,"value":1542},"函数将",{"type":18,"tag":42,"props":1544,"children":1546},{"className":1545},[],[1547],{"type":28,"value":1533},{"type":28,"value":1549},"文件打开，则可以使用下述代码：",{"type":18,"tag":51,"props":1551,"children":1553},{"className":53,"code":1552,"language":55,"meta":7,"style":7},"with open(\"info\u002Fdemo.txt\",'r') as f:\n    message = f.read()\n",[1554],{"type":18,"tag":42,"props":1555,"children":1556},{"__ignoreMap":7},[1557,1565],{"type":18,"tag":61,"props":1558,"children":1559},{"class":63,"line":64},[1560],{"type":18,"tag":61,"props":1561,"children":1562},{},[1563],{"type":28,"value":1564},"with open(\"info\u002Fdemo.txt\",'r') as f:\n",{"type":18,"tag":61,"props":1566,"children":1567},{"class":63,"line":563},[1568],{"type":18,"tag":61,"props":1569,"children":1570},{},[1571],{"type":28,"value":1572},"    message = f.read()\n",{"type":18,"tag":36,"props":1574,"children":1575},{},[1576,1578,1583],{"type":28,"value":1577},"而",{"type":18,"tag":751,"props":1579,"children":1580},{},[1581],{"type":28,"value":1582},"绝对路径",{"type":28,"value":1584},"的方式如下所示：",{"type":18,"tag":51,"props":1586,"children":1588},{"className":53,"code":1587,"language":55,"meta":7,"style":7},"with open(\"D:file\u002Fcode\u002Finfo\u002Fdemo.txt\",'r') as f:\n    message = f.read()\n",[1589],{"type":18,"tag":42,"props":1590,"children":1591},{"__ignoreMap":7},[1592,1600],{"type":18,"tag":61,"props":1593,"children":1594},{"class":63,"line":64},[1595],{"type":18,"tag":61,"props":1596,"children":1597},{},[1598],{"type":28,"value":1599},"with open(\"D:file\u002Fcode\u002Finfo\u002Fdemo.txt\",'r') as f:\n",{"type":18,"tag":61,"props":1601,"children":1602},{"class":63,"line":563},[1603],{"type":18,"tag":61,"props":1604,"children":1605},{},[1606],{"type":28,"value":1572},{"type":18,"tag":499,"props":1608,"children":1609},{},[1610],{"type":18,"tag":36,"props":1611,"children":1612},{},[1613,1615,1621,1623,1629,1631,1636,1638,1644,1646,1651,1653,1658],{"type":28,"value":1614},"在指定文件路径时，需要对路径中的 ",{"type":18,"tag":42,"props":1616,"children":1618},{"className":1617},[],[1619],{"type":28,"value":1620},"\\",{"type":28,"value":1622}," 使用 ",{"type":18,"tag":42,"props":1624,"children":1626},{"className":1625},[],[1627],{"type":28,"value":1628},"\\\\",{"type":28,"value":1630}," 进行转义操作，或者将 ",{"type":18,"tag":42,"props":1632,"children":1634},{"className":1633},[],[1635],{"type":28,"value":1620},{"type":28,"value":1637}," 替换为 ",{"type":18,"tag":42,"props":1639,"children":1641},{"className":1640},[],[1642],{"type":28,"value":1643},"\u002F",{"type":28,"value":1645},"，若不想修改",{"type":18,"tag":42,"props":1647,"children":1649},{"className":1648},[],[1650],{"type":28,"value":1620},{"type":28,"value":1652},"，也可以在路径字符串前加一个",{"type":18,"tag":42,"props":1654,"children":1656},{"className":1655},[],[1657],{"type":28,"value":113},{"type":28,"value":1659},"。",{"type":18,"tag":36,"props":1661,"children":1662},{},[1663,1665,1670,1672,1678],{"type":28,"value":1664},"若要将两个或者多个路径拼接到一起组成一个新的路径，可以使用",{"type":18,"tag":42,"props":1666,"children":1668},{"className":1667},[],[1669],{"type":28,"value":1083},{"type":28,"value":1671},"模块的",{"type":18,"tag":42,"props":1673,"children":1675},{"className":1674},[],[1676],{"type":28,"value":1677},"join()",{"type":28,"value":1679},"函数来实现，其具体语法如下：",{"type":18,"tag":51,"props":1681,"children":1683},{"className":53,"code":1682,"language":55,"meta":7,"style":7},"os.path.join(path1[,path2,path3...])\n",[1684],{"type":18,"tag":42,"props":1685,"children":1686},{"__ignoreMap":7},[1687],{"type":18,"tag":61,"props":1688,"children":1689},{"class":63,"line":64},[1690],{"type":18,"tag":61,"props":1691,"children":1692},{},[1693],{"type":28,"value":1682},{"type":18,"tag":36,"props":1695,"children":1696},{},[1697,1703,1705,1711],{"type":18,"tag":42,"props":1698,"children":1700},{"className":1699},[],[1701],{"type":28,"value":1702},"path1",{"type":28,"value":1704},"、",{"type":18,"tag":42,"props":1706,"children":1708},{"className":1707},[],[1709],{"type":28,"value":1710},"path2",{"type":28,"value":1712},"等参数代表路径，若该参数中不存在绝对路径，那么最后拼接的结果为一个相对路径。该过程不会检测路径是否存在。",{"type":18,"tag":499,"props":1714,"children":1715},{},[1716],{"type":18,"tag":36,"props":1717,"children":1718},{},[1719,1721,1726],{"type":28,"value":1720},"在使用",{"type":18,"tag":42,"props":1722,"children":1724},{"className":1723},[],[1725],{"type":28,"value":1677},{"type":28,"value":1727},"函数拼接时，若存在多个绝对路径，那么将以最靠右侧的绝对路径为主，并且该路径之前的参数都将忽视。",{"type":18,"tag":30,"props":1729,"children":1731},{"id":1730},"判断目录是否存在",[1732],{"type":28,"value":1730},{"type":18,"tag":36,"props":1734,"children":1735},{},[1736,1738,1743,1745,1751],{"type":28,"value":1737},"当需要判断路径是否存在时，可以使用",{"type":18,"tag":42,"props":1739,"children":1741},{"className":1740},[],[1742],{"type":28,"value":1083},{"type":28,"value":1744},"模块提供的",{"type":18,"tag":42,"props":1746,"children":1748},{"className":1747},[],[1749],{"type":28,"value":1750},"exists()",{"type":28,"value":1752},"函数，该函数除了可以判断目标是否存在，还可以判断文件是否存在。用法如下：",{"type":18,"tag":51,"props":1754,"children":1756},{"className":53,"code":1755,"language":55,"meta":7,"style":7},"import os\nos.path.exists(path)\n",[1757],{"type":18,"tag":42,"props":1758,"children":1759},{"__ignoreMap":7},[1760,1767],{"type":18,"tag":61,"props":1761,"children":1762},{"class":63,"line":64},[1763],{"type":18,"tag":61,"props":1764,"children":1765},{},[1766],{"type":28,"value":1485},{"type":18,"tag":61,"props":1768,"children":1769},{"class":63,"line":563},[1770],{"type":18,"tag":61,"props":1771,"children":1772},{},[1773],{"type":28,"value":1774},"os.path.exists(path)\n",{"type":18,"tag":36,"props":1776,"children":1777},{},[1778,1784,1786,1792],{"type":18,"tag":42,"props":1779,"children":1781},{"className":1780},[],[1782],{"type":28,"value":1783},"path",{"type":28,"value":1785},"为需要判断的路径，绝对或相对路径任意，返回值为",{"type":18,"tag":42,"props":1787,"children":1789},{"className":1788},[],[1790],{"type":28,"value":1791},"bool",{"type":28,"value":1793},"类型。",{"type":18,"tag":30,"props":1795,"children":1796},{"id":1214},[1797],{"type":28,"value":1214},{"type":18,"tag":36,"props":1799,"children":1800},{},[1801,1803,1808],{"type":28,"value":1802},"Python中的",{"type":18,"tag":42,"props":1804,"children":1806},{"className":1805},[],[1807],{"type":28,"value":1075},{"type":28,"value":1809},"模块提供了两个创建目录的方式：",{"type":18,"tag":71,"props":1811,"children":1812},{},[1813,1824],{"type":18,"tag":75,"props":1814,"children":1815},{},[1816,1818],{"type":28,"value":1817},"创建一级目录 ",{"type":18,"tag":42,"props":1819,"children":1821},{"className":1820},[],[1822],{"type":28,"value":1823},"os.mkdir()",{"type":18,"tag":75,"props":1825,"children":1826},{},[1827,1828],{"type":28,"value":1231},{"type":18,"tag":42,"props":1829,"children":1831},{"className":1830},[],[1832],{"type":28,"value":1833},"os.makedirs()",{"type":18,"tag":36,"props":1835,"children":1836},{},[1837],{"type":18,"tag":751,"props":1838,"children":1839},{},[1840],{"type":28,"value":1841},"创建一级目录",{"type":18,"tag":36,"props":1843,"children":1844},{},[1845,1847,1852,1854,1860],{"type":28,"value":1846},"创建一级目录表示一次只能创建一级目录，可以通过",{"type":18,"tag":42,"props":1848,"children":1850},{"className":1849},[],[1851],{"type":28,"value":1823},{"type":28,"value":1853},"实现，若该目录上级不存在，则抛出",{"type":18,"tag":42,"props":1855,"children":1857},{"className":1856},[],[1858],{"type":28,"value":1859},"FileNotFoundError",{"type":28,"value":1861},"异常信息。其语法如下：",{"type":18,"tag":51,"props":1863,"children":1865},{"className":53,"code":1864,"language":55,"meta":7,"style":7},"os.mkdir(path,mode=0o777)\n",[1866],{"type":18,"tag":42,"props":1867,"children":1868},{"__ignoreMap":7},[1869],{"type":18,"tag":61,"props":1870,"children":1871},{"class":63,"line":64},[1872],{"type":18,"tag":61,"props":1873,"children":1874},{},[1875],{"type":28,"value":1864},{"type":18,"tag":71,"props":1877,"children":1878},{},[1879,1889],{"type":18,"tag":75,"props":1880,"children":1881},{},[1882,1887],{"type":18,"tag":42,"props":1883,"children":1885},{"className":1884},[],[1886],{"type":28,"value":1783},{"type":28,"value":1888},"：用户指定要创建的目录，绝对或相对路径任意",{"type":18,"tag":75,"props":1890,"children":1891},{},[1892,1897,1899,1905],{"type":18,"tag":42,"props":1893,"children":1895},{"className":1894},[],[1896],{"type":28,"value":105},{"type":28,"value":1898},"：用于指定数值模式，默认值为",{"type":18,"tag":42,"props":1900,"children":1902},{"className":1901},[],[1903],{"type":28,"value":1904},"0o777",{"type":28,"value":1906},"，该参数非Unix系统将被忽视或无效。",{"type":18,"tag":36,"props":1908,"children":1909},{},[1910],{"type":18,"tag":751,"props":1911,"children":1912},{},[1913],{"type":28,"value":1231},{"type":18,"tag":36,"props":1915,"children":1916},{},[1917,1919,1924],{"type":28,"value":1918},"可以通过",{"type":18,"tag":42,"props":1920,"children":1922},{"className":1921},[],[1923],{"type":28,"value":1833},{"type":28,"value":1925},"实现创建多级目录，该函数通过采用递归的方式创建目录。其语法如下：",{"type":18,"tag":51,"props":1927,"children":1928},{"className":53,"code":1864,"language":55,"meta":7,"style":7},[1929],{"type":18,"tag":42,"props":1930,"children":1931},{"__ignoreMap":7},[1932],{"type":18,"tag":61,"props":1933,"children":1934},{"class":63,"line":64},[1935],{"type":18,"tag":61,"props":1936,"children":1937},{},[1938],{"type":28,"value":1864},{"type":18,"tag":71,"props":1940,"children":1941},{},[1942,1951],{"type":18,"tag":75,"props":1943,"children":1944},{},[1945,1950],{"type":18,"tag":42,"props":1946,"children":1948},{"className":1947},[],[1949],{"type":28,"value":1783},{"type":28,"value":1888},{"type":18,"tag":75,"props":1952,"children":1953},{},[1954,1959,1960,1965],{"type":18,"tag":42,"props":1955,"children":1957},{"className":1956},[],[1958],{"type":28,"value":105},{"type":28,"value":1898},{"type":18,"tag":42,"props":1961,"children":1963},{"className":1962},[],[1964],{"type":28,"value":1904},{"type":28,"value":1906},{"type":18,"tag":30,"props":1967,"children":1968},{"id":1248},[1969],{"type":28,"value":1248},{"type":18,"tag":36,"props":1971,"children":1972},{},[1973,1975,1980,1981,1987],{"type":28,"value":1974},"删除目录可以使用",{"type":18,"tag":42,"props":1976,"children":1978},{"className":1977},[],[1979],{"type":28,"value":1075},{"type":28,"value":1671},{"type":18,"tag":42,"props":1982,"children":1984},{"className":1983},[],[1985],{"type":28,"value":1986},"rmdir()",{"type":28,"value":1988},"函数来实现，只有当前要删除的文件为空时才生效，其语法如下 ：",{"type":18,"tag":51,"props":1990,"children":1992},{"className":53,"code":1991,"language":55,"meta":7,"style":7},"import os\nos.rmdir(path)\n",[1993],{"type":18,"tag":42,"props":1994,"children":1995},{"__ignoreMap":7},[1996,2003],{"type":18,"tag":61,"props":1997,"children":1998},{"class":63,"line":64},[1999],{"type":18,"tag":61,"props":2000,"children":2001},{},[2002],{"type":28,"value":1485},{"type":18,"tag":61,"props":2004,"children":2005},{"class":63,"line":563},[2006],{"type":18,"tag":61,"props":2007,"children":2008},{},[2009],{"type":28,"value":2010},"os.rmdir(path)\n",{"type":18,"tag":36,"props":2012,"children":2013},{},[2014,2016,2021,2023,2028],{"type":28,"value":2015},"若目录不存在，将抛出",{"type":18,"tag":42,"props":2017,"children":2019},{"className":2018},[],[2020],{"type":28,"value":1859},{"type":28,"value":2022},"异常信息，因此该函数可以于",{"type":18,"tag":42,"props":2024,"children":2026},{"className":2025},[],[2027],{"type":28,"value":1750},{"type":28,"value":2029},"函数联合使用。",{"type":18,"tag":499,"props":2031,"children":2032},{},[2033],{"type":18,"tag":36,"props":2034,"children":2035},{},[2036,2038,2044,2046,2052],{"type":28,"value":2037},"若要删除非空目录，需要使用Python内置模块",{"type":18,"tag":42,"props":2039,"children":2041},{"className":2040},[],[2042],{"type":28,"value":2043},"shutill",{"type":28,"value":2045},"的",{"type":18,"tag":42,"props":2047,"children":2049},{"className":2048},[],[2050],{"type":28,"value":2051},"rmtree()",{"type":28,"value":2053},"函数实现。",{"type":18,"tag":30,"props":2055,"children":2057},{"id":2056},"遍历目录",[2058],{"type":28,"value":2056},{"type":18,"tag":36,"props":2060,"children":2061},{},[2062,2064,2069,2070,2076],{"type":28,"value":2063},"对指定的目录下的全部目录及文件运行一遍。在Python中，",{"type":18,"tag":42,"props":2065,"children":2067},{"className":2066},[],[2068],{"type":28,"value":1075},{"type":28,"value":2045},{"type":18,"tag":42,"props":2071,"children":2073},{"className":2072},[],[2074],{"type":28,"value":2075},"walk()",{"type":28,"value":2077},"函数提供了遍历目录的功能，其语法格式如下：",{"type":18,"tag":51,"props":2079,"children":2081},{"className":53,"code":2080,"language":55,"meta":7,"style":7},"import os\nos.walk(top[,topdown][, onerror][, followlinks])\n",[2082],{"type":18,"tag":42,"props":2083,"children":2084},{"__ignoreMap":7},[2085,2092],{"type":18,"tag":61,"props":2086,"children":2087},{"class":63,"line":64},[2088],{"type":18,"tag":61,"props":2089,"children":2090},{},[2091],{"type":28,"value":1485},{"type":18,"tag":61,"props":2093,"children":2094},{"class":63,"line":563},[2095],{"type":18,"tag":61,"props":2096,"children":2097},{},[2098],{"type":28,"value":2099},"os.walk(top[,topdown][, onerror][, followlinks])\n",{"type":18,"tag":71,"props":2101,"children":2102},{},[2103,2114,2146,2157,2182],{"type":18,"tag":75,"props":2104,"children":2105},{},[2106,2112],{"type":18,"tag":42,"props":2107,"children":2109},{"className":2108},[],[2110],{"type":28,"value":2111},"top",{"type":28,"value":2113},"：用于指定要遍历内容的根目录",{"type":18,"tag":75,"props":2115,"children":2116},{},[2117,2123,2125,2131,2133,2139,2141],{"type":18,"tag":42,"props":2118,"children":2120},{"className":2119},[],[2121],{"type":28,"value":2122},"topdown",{"type":28,"value":2124},"：可选参数，指定遍历列表的顺序，",{"type":18,"tag":42,"props":2126,"children":2128},{"className":2127},[],[2129],{"type":28,"value":2130},"True",{"type":28,"value":2132},"表示自上而下遍历（先遍历根目录），",{"type":18,"tag":42,"props":2134,"children":2136},{"className":2135},[],[2137],{"type":28,"value":2138},"False",{"type":28,"value":2140},"表示自下而上遍历（先遍历最后一级子目录），默认为",{"type":18,"tag":42,"props":2142,"children":2144},{"className":2143},[],[2145],{"type":28,"value":2130},{"type":18,"tag":75,"props":2147,"children":2148},{},[2149,2155],{"type":18,"tag":42,"props":2150,"children":2152},{"className":2151},[],[2153],{"type":28,"value":2154},"onerror",{"type":28,"value":2156},"：可选参数，指定错误处理方式，默认为忽略，若不想默认，可自行采用函数。",{"type":18,"tag":75,"props":2158,"children":2159},{},[2160,2166,2168,2173,2175,2180],{"type":18,"tag":42,"props":2161,"children":2163},{"className":2162},[],[2164],{"type":28,"value":2165},"followlinks",{"type":28,"value":2167},"：可选参数，默认情况下，",{"type":18,"tag":42,"props":2169,"children":2171},{"className":2170},[],[2172],{"type":28,"value":2075},{"type":28,"value":2174},"函数不会向下转换成解析到目录的符号链接，将该函数值设置为",{"type":18,"tag":42,"props":2176,"children":2178},{"className":2177},[],[2179],{"type":28,"value":2130},{"type":28,"value":2181},"，表示用于指定在支持的系统上访问由符号链接指向的目录。",{"type":18,"tag":75,"props":2183,"children":2184},{},[2185,2187,2193,2194,2200,2201,2207,2209,2214,2216,2221,2223,2228],{"type":28,"value":2186},"返回值：返回一个包含三个元素（",{"type":18,"tag":42,"props":2188,"children":2190},{"className":2189},[],[2191],{"type":28,"value":2192},"dirpath",{"type":28,"value":1704},{"type":18,"tag":42,"props":2195,"children":2197},{"className":2196},[],[2198],{"type":28,"value":2199},"dirnames",{"type":28,"value":1704},{"type":18,"tag":42,"props":2202,"children":2204},{"className":2203},[],[2205],{"type":28,"value":2206},"filenames",{"type":28,"value":2208},"）的元组生成器对象。其中",{"type":18,"tag":42,"props":2210,"children":2212},{"className":2211},[],[2213],{"type":28,"value":2192},{"type":28,"value":2215},"表示当前遍历的路径，",{"type":18,"tag":42,"props":2217,"children":2219},{"className":2218},[],[2220],{"type":28,"value":2199},{"type":28,"value":2222},"表示当前路径下包含的子目录，是一个列表，",{"type":18,"tag":42,"props":2224,"children":2226},{"className":2225},[],[2227],{"type":28,"value":2206},{"type":28,"value":2229},"表示当前路径下包含的文件，是一个列表。",{"type":18,"tag":499,"props":2231,"children":2232},{},[2233],{"type":18,"tag":36,"props":2234,"children":2235},{},[2236,2241],{"type":18,"tag":42,"props":2237,"children":2239},{"className":2238},[],[2240],{"type":28,"value":2075},{"type":28,"value":2242},"函数只在UNIX和Windows系统有效",{"type":18,"tag":23,"props":2244,"children":2246},{"id":2245},"高级文件操作",[2247],{"type":28,"value":2245},{"type":18,"tag":36,"props":2249,"children":2250},{},[2251,2256],{"type":18,"tag":42,"props":2252,"children":2254},{"className":2253},[],[2255],{"type":28,"value":1075},{"type":28,"value":2257},"模块提供的与文件相关的函数及其说明如表：",{"type":18,"tag":138,"props":2259,"children":2260},{},[2261,2275],{"type":18,"tag":142,"props":2262,"children":2263},{},[2264],{"type":18,"tag":146,"props":2265,"children":2266},{},[2267,2271],{"type":18,"tag":150,"props":2268,"children":2269},{"align":152},[2270],{"type":28,"value":590},{"type":18,"tag":150,"props":2272,"children":2273},{"align":152},[2274],{"type":28,"value":160},{"type":18,"tag":167,"props":2276,"children":2277},{},[2278,2295,2312,2329,2346,2363],{"type":18,"tag":146,"props":2279,"children":2280},{},[2281,2290],{"type":18,"tag":174,"props":2282,"children":2283},{"align":152},[2284],{"type":18,"tag":42,"props":2285,"children":2287},{"className":2286},[],[2288],{"type":28,"value":2289},"access(path,accessmode)",{"type":18,"tag":174,"props":2291,"children":2292},{"align":152},[2293],{"type":28,"value":2294},"获取文件访问权限（读取\u002F写入\u002F执行权限），值为R_OK(读取)、W_OK(写入)、X_OK(执行)、F_OK(存在)，若权限都存在，则返回1，否则返回0",{"type":18,"tag":146,"props":2296,"children":2297},{},[2298,2307],{"type":18,"tag":174,"props":2299,"children":2300},{"align":152},[2301],{"type":18,"tag":42,"props":2302,"children":2304},{"className":2303},[],[2305],{"type":28,"value":2306},"chmod(path,mode)",{"type":18,"tag":174,"props":2308,"children":2309},{"align":152},[2310],{"type":28,"value":2311},"修改path指定文件的访问权限",{"type":18,"tag":146,"props":2313,"children":2314},{},[2315,2324],{"type":18,"tag":174,"props":2316,"children":2317},{"align":152},[2318],{"type":18,"tag":42,"props":2319,"children":2321},{"className":2320},[],[2322],{"type":28,"value":2323},"remove(path)",{"type":18,"tag":174,"props":2325,"children":2326},{"align":152},[2327],{"type":28,"value":2328},"删除path指定的文件路径",{"type":18,"tag":146,"props":2330,"children":2331},{},[2332,2341],{"type":18,"tag":174,"props":2333,"children":2334},{"align":152},[2335],{"type":18,"tag":42,"props":2336,"children":2338},{"className":2337},[],[2339],{"type":28,"value":2340},"rename(src,dst)",{"type":18,"tag":174,"props":2342,"children":2343},{"align":152},[2344],{"type":28,"value":2345},"将文件或目录src重命名为dst",{"type":18,"tag":146,"props":2347,"children":2348},{},[2349,2358],{"type":18,"tag":174,"props":2350,"children":2351},{"align":152},[2352],{"type":18,"tag":42,"props":2353,"children":2355},{"className":2354},[],[2356],{"type":28,"value":2357},"stat(path)",{"type":18,"tag":174,"props":2359,"children":2360},{"align":152},[2361],{"type":28,"value":2362},"返回path指定文件的信息",{"type":18,"tag":146,"props":2364,"children":2365},{},[2366,2375],{"type":18,"tag":174,"props":2367,"children":2368},{"align":152},[2369],{"type":18,"tag":42,"props":2370,"children":2372},{"className":2371},[],[2373],{"type":28,"value":2374},"startfile(path[, operation])",{"type":18,"tag":174,"props":2376,"children":2377},{"align":152},[2378],{"type":28,"value":2379},"使用关联的应用程序打开path指定的文件",{"type":18,"tag":499,"props":2381,"children":2382},{},[2383],{"type":18,"tag":36,"props":2384,"children":2385},{},[2386,2387,2393],{"type":28,"value":1720},{"type":18,"tag":42,"props":2388,"children":2390},{"className":2389},[],[2391],{"type":28,"value":2392},"rename()",{"type":28,"value":2394},"函数重命名目录时，只能修改最后一级的目录名称，否则将抛出异常",{"type":18,"tag":30,"props":2396,"children":2398},{"id":2397},"获取文件基本信息",[2399],{"type":28,"value":2397},{"type":18,"tag":36,"props":2401,"children":2402},{},[2403,2405,2410,2411,2417],{"type":28,"value":2404},"在Python中，可以通过",{"type":18,"tag":42,"props":2406,"children":2408},{"className":2407},[],[2409],{"type":28,"value":1075},{"type":28,"value":1671},{"type":18,"tag":42,"props":2412,"children":2414},{"className":2413},[],[2415],{"type":28,"value":2416},"stat()",{"type":28,"value":2418},"函数获取文件的基本信息。其格式语法如下：",{"type":18,"tag":51,"props":2420,"children":2422},{"className":53,"code":2421,"language":55,"meta":7,"style":7},"import os\nprint(os.stat(path))\n",[2423],{"type":18,"tag":42,"props":2424,"children":2425},{"__ignoreMap":7},[2426,2433],{"type":18,"tag":61,"props":2427,"children":2428},{"class":63,"line":64},[2429],{"type":18,"tag":61,"props":2430,"children":2431},{},[2432],{"type":28,"value":1485},{"type":18,"tag":61,"props":2434,"children":2435},{"class":63,"line":563},[2436],{"type":18,"tag":61,"props":2437,"children":2438},{},[2439],{"type":28,"value":2440},"print(os.stat(path))\n",{"type":18,"tag":36,"props":2442,"children":2443},{},[2444,2450],{"type":18,"tag":42,"props":2445,"children":2447},{"className":2446},[],[2448],{"type":28,"value":2449},"stat()函数",{"type":28,"value":2451}," 返回的对象的常用属性及其说明",{"type":18,"tag":138,"props":2453,"children":2454},{},[2455,2470],{"type":18,"tag":142,"props":2456,"children":2457},{},[2458],{"type":18,"tag":146,"props":2459,"children":2460},{},[2461,2466],{"type":18,"tag":150,"props":2462,"children":2463},{"align":152},[2464],{"type":28,"value":2465},"属性",{"type":18,"tag":150,"props":2467,"children":2468},{"align":152},[2469],{"type":28,"value":160},{"type":18,"tag":167,"props":2471,"children":2472},{},[2473,2490,2507,2524,2541,2558,2575,2592,2609,2626],{"type":18,"tag":146,"props":2474,"children":2475},{},[2476,2485],{"type":18,"tag":174,"props":2477,"children":2478},{"align":152},[2479],{"type":18,"tag":42,"props":2480,"children":2482},{"className":2481},[],[2483],{"type":28,"value":2484},"st_mode",{"type":18,"tag":174,"props":2486,"children":2487},{"align":152},[2488],{"type":28,"value":2489},"保护模式",{"type":18,"tag":146,"props":2491,"children":2492},{},[2493,2502],{"type":18,"tag":174,"props":2494,"children":2495},{"align":152},[2496],{"type":18,"tag":42,"props":2497,"children":2499},{"className":2498},[],[2500],{"type":28,"value":2501},"st_ino",{"type":18,"tag":174,"props":2503,"children":2504},{"align":152},[2505],{"type":28,"value":2506},"索引号",{"type":18,"tag":146,"props":2508,"children":2509},{},[2510,2519],{"type":18,"tag":174,"props":2511,"children":2512},{"align":152},[2513],{"type":18,"tag":42,"props":2514,"children":2516},{"className":2515},[],[2517],{"type":28,"value":2518},"st_nlink",{"type":18,"tag":174,"props":2520,"children":2521},{"align":152},[2522],{"type":28,"value":2523},"硬链接号（被链接数目）",{"type":18,"tag":146,"props":2525,"children":2526},{},[2527,2536],{"type":18,"tag":174,"props":2528,"children":2529},{"align":152},[2530],{"type":18,"tag":42,"props":2531,"children":2533},{"className":2532},[],[2534],{"type":28,"value":2535},"st_size",{"type":18,"tag":174,"props":2537,"children":2538},{"align":152},[2539],{"type":28,"value":2540},"文件大小，单位为Byte",{"type":18,"tag":146,"props":2542,"children":2543},{},[2544,2553],{"type":18,"tag":174,"props":2545,"children":2546},{"align":152},[2547],{"type":18,"tag":42,"props":2548,"children":2550},{"className":2549},[],[2551],{"type":28,"value":2552},"st_mtime",{"type":18,"tag":174,"props":2554,"children":2555},{"align":152},[2556],{"type":28,"value":2557},"最后一次修改时间",{"type":18,"tag":146,"props":2559,"children":2560},{},[2561,2570],{"type":18,"tag":174,"props":2562,"children":2563},{"align":152},[2564],{"type":18,"tag":42,"props":2565,"children":2567},{"className":2566},[],[2568],{"type":28,"value":2569},"st_dev",{"type":18,"tag":174,"props":2571,"children":2572},{"align":152},[2573],{"type":28,"value":2574},"设备名",{"type":18,"tag":146,"props":2576,"children":2577},{},[2578,2587],{"type":18,"tag":174,"props":2579,"children":2580},{"align":152},[2581],{"type":18,"tag":42,"props":2582,"children":2584},{"className":2583},[],[2585],{"type":28,"value":2586},"st_uid",{"type":18,"tag":174,"props":2588,"children":2589},{"align":152},[2590],{"type":28,"value":2591},"用户ID",{"type":18,"tag":146,"props":2593,"children":2594},{},[2595,2604],{"type":18,"tag":174,"props":2596,"children":2597},{"align":152},[2598],{"type":18,"tag":42,"props":2599,"children":2601},{"className":2600},[],[2602],{"type":28,"value":2603},"st_gid",{"type":18,"tag":174,"props":2605,"children":2606},{"align":152},[2607],{"type":28,"value":2608},"组ID",{"type":18,"tag":146,"props":2610,"children":2611},{},[2612,2621],{"type":18,"tag":174,"props":2613,"children":2614},{"align":152},[2615],{"type":18,"tag":42,"props":2616,"children":2618},{"className":2617},[],[2619],{"type":28,"value":2620},"st_atime",{"type":18,"tag":174,"props":2622,"children":2623},{"align":152},[2624],{"type":28,"value":2625},"最后一次访问时间",{"type":18,"tag":146,"props":2627,"children":2628},{},[2629,2638],{"type":18,"tag":174,"props":2630,"children":2631},{"align":152},[2632],{"type":18,"tag":42,"props":2633,"children":2635},{"className":2634},[],[2636],{"type":28,"value":2637},"st_ctime",{"type":18,"tag":174,"props":2639,"children":2640},{"align":152},[2641],{"type":28,"value":2642},"最后一次状态变化的时间。系统不同，返回结果也不同",{"type":18,"tag":36,"props":2644,"children":2645},{},[2646],{"type":28,"value":2647},"如下示例所示：",{"type":18,"tag":51,"props":2649,"children":2651},{"className":53,"code":2650,"language":55,"meta":7,"style":7},"import os\nfileinfo = os.stat(\"demo.png\")\nprint(\"文件的完整路径：\",os.path.abspath(\"demo.png\"))\nprint(\"索引号：\",fileinfo.st_ino)\nprint(\"设备名：\",fileinfo.st_dev)\nprint(\"文件大小：\",fileinfo.st_size,\"Byte\")\nprint(\"最后一次访问时间：\",fileinfo.st_atime)\nprint(\"最后一次修改时间：\",fileinfo.st_mtime)\nprint(\"最后一次状态变化时间：\",fileinfo.st_ctime)\n",[2652],{"type":18,"tag":42,"props":2653,"children":2654},{"__ignoreMap":7},[2655,2662,2670,2678,2686,2695,2704,2713,2722],{"type":18,"tag":61,"props":2656,"children":2657},{"class":63,"line":64},[2658],{"type":18,"tag":61,"props":2659,"children":2660},{},[2661],{"type":28,"value":1485},{"type":18,"tag":61,"props":2663,"children":2664},{"class":63,"line":563},[2665],{"type":18,"tag":61,"props":2666,"children":2667},{},[2668],{"type":28,"value":2669},"fileinfo = os.stat(\"demo.png\")\n",{"type":18,"tag":61,"props":2671,"children":2672},{"class":63,"line":976},[2673],{"type":18,"tag":61,"props":2674,"children":2675},{},[2676],{"type":28,"value":2677},"print(\"文件的完整路径：\",os.path.abspath(\"demo.png\"))\n",{"type":18,"tag":61,"props":2679,"children":2680},{"class":63,"line":985},[2681],{"type":18,"tag":61,"props":2682,"children":2683},{},[2684],{"type":28,"value":2685},"print(\"索引号：\",fileinfo.st_ino)\n",{"type":18,"tag":61,"props":2687,"children":2689},{"class":63,"line":2688},5,[2690],{"type":18,"tag":61,"props":2691,"children":2692},{},[2693],{"type":28,"value":2694},"print(\"设备名：\",fileinfo.st_dev)\n",{"type":18,"tag":61,"props":2696,"children":2698},{"class":63,"line":2697},6,[2699],{"type":18,"tag":61,"props":2700,"children":2701},{},[2702],{"type":28,"value":2703},"print(\"文件大小：\",fileinfo.st_size,\"Byte\")\n",{"type":18,"tag":61,"props":2705,"children":2707},{"class":63,"line":2706},7,[2708],{"type":18,"tag":61,"props":2709,"children":2710},{},[2711],{"type":28,"value":2712},"print(\"最后一次访问时间：\",fileinfo.st_atime)\n",{"type":18,"tag":61,"props":2714,"children":2716},{"class":63,"line":2715},8,[2717],{"type":18,"tag":61,"props":2718,"children":2719},{},[2720],{"type":28,"value":2721},"print(\"最后一次修改时间：\",fileinfo.st_mtime)\n",{"type":18,"tag":61,"props":2723,"children":2725},{"class":63,"line":2724},9,[2726],{"type":18,"tag":61,"props":2727,"children":2728},{},[2729],{"type":28,"value":2730},"print(\"最后一次状态变化时间：\",fileinfo.st_ctime)\n",{"type":18,"tag":2732,"props":2733,"children":2734},"style",{},[2735],{"type":28,"value":2736},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":563,"depth":563,"links":2738},[2739,2740,2741],{"id":25,"depth":976,"text":25},{"id":1063,"depth":976,"text":1063},{"id":2245,"depth":976,"text":2245},"markdown","content:blog:2025-07-09-python-文件及目录操作.md","content","blog\u002F2025-07-09-python-文件及目录操作.md","blog\u002F2025-07-09-python-文件及目录操作","md",1780801018422]