簡體   English   中英

用python打開.txt文件

[英]Open .txt file with python

所以我很困惑為什么這個名為common.txt的簡單記事本文件沒有打開。 我想知道記事本是否可以在python中使用。

所以我想說文件是否存在:

import os.path
import sys

def file_exist(common):

#test whether the file exists and open it to a data structure

    if os.path.isfile(common):
       return common

    else:
        return

def main():
    common = input("enter: ")
    print(file_exist(common))

if __name__ == "__main__":
    main()

我用csv文件做了相同的代碼,它似乎工作。

輸出是:

enter: common.txt
None

這段代碼對我來說很好。

我將其保存為common.py,然后從common.py所在目錄中的命令行運行它。

這是會議:

C:\temp\so> python common.py
enter: common.py
common.py

代碼執行以下操作: - 請求輸入 - 如果輸入是工作目錄中的文件名,則打印出文件名。 - 如果輸入不是工作目錄中的文件名,則輸出None。

你還想要這個代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM