第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

蟒蛇中的文件大小不正確

蟒蛇中的文件大小不正確

炎炎設(shè)計 2022-10-05 09:36:36
import osdef create_python_script(filename):    comments = "# Start of a new Python Program"    #filesize = 0    with open(filename, 'w') as new_file:        new_file.write(comments)        cwd=os.getcwd()        fpath = os.path.abspath(filename)        filesize=os.path.getsize(fpath)    return(filesize)print(create_python_script('newprogram.py'))我得到的結(jié)果為零,但它應(yīng)該得到“31”
查看完整描述

4 回答

?
BIG陽

TA貢獻(xiàn)1859條經(jīng)驗 獲得超6個贊

在嘗試獲取文件大小之前,您沒有關(guān)閉文件,就像在塊內(nèi)所做的那樣。把它帶到外面:with


import os


def create_python_script(filename):

    comments = "# Start of a new Python Program"

    #filesize = 0

    with open(filename, 'w') as new_file:

        new_file.write(comments)

        cwd=os.getcwd()

        fpath = os.path.abspath(filename)

        print(fpath)


    filesize=os.path.getsize(fpath)

    return(filesize)


print(create_python_script('newprogram.py'))

# 31


查看完整回答
反對 回復(fù) 2022-10-05
?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗 獲得超18個贊

import os


def create_python_script(filename):

  comments = "# Start of a new Python program"

  with open(filename, 'w') as file:

    file.write(comments)

    file.close()

    filepath = os.path.abspath(filename)

    filesize = os.path.getsize(filepath)

  return(filesize)


print(create_python_script("program.py"))


#this will give you correct result


查看完整回答
反對 回復(fù) 2022-10-05
?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗 獲得超5個贊

這個也工作得很好!


    def create_python_script(filename):

      import os

      comments = "# Start of a new Python program"

      with open(filename,'w')as file:

         file.write(comments)

      filesize = os.path.getsize(filename)

      return(filesize)

    print(create_python_script("program.py"))


查看完整回答
反對 回復(fù) 2022-10-05
?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗 獲得超9個贊

首先打開具有寫入權(quán)限的文件,以在文件中添加文本。然后以讀取權(quán)限打開文件以獲取文件的大小。


import os


def create_python_script(filename):

    comments = "# Start of a new Python program"

    with open(filename, 'w') as pd:

        pd.write(comments)


    with  open(filename, "r"):

        filesize = os.path.getsize(filename)

        print(filesize)

    return filesize


print(create_python_script("program.py"))


查看完整回答
反對 回復(fù) 2022-10-05
  • 4 回答
  • 0 關(guān)注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號