![](/img/trans.png)
[英]Is there a more pythonic way of storing parameters so they can be used in a function call?
[英]how can i call the file testo.txt, directly from the function argument? what's a more pythonic way?
path='/home/quattro/file python /L9/files/'
testo=open(path+'testo.txt', encoding='utf8')
def clines(file, s):
cnt=0
with file as f:
for i in f:
cnt+=1
return cnt
print(clines(testo, 'err'))
如果我只是把 'testo.txt' id 作為參數扔給我:
[Errno 2] No such file or directory: 'testo.txt'
什么是更全局的解決方案,而不必進行整個路徑分配?
獎金問題:我在哪里可以學習在我的編程中更加 pitonic 而不是像堆棧溢出中的垃圾郵件問題? 謝謝
獎金問題:我在哪里可以學習在我的編程中更加 pitonic 而不是像堆棧溢出中的垃圾郵件問題? 謝謝
文件夾名稱'file python '
末尾的空格是多少?
首先檢查目錄是否存在
path_exists = os.path.exists(path)
print(f"{path_exists=}")
結果
path_exists=True
然后檢查路徑和文件
file_exists = os.path.exists(path + file)
print(f"{file_exists=}")
和這里
file_exists=False
了解它是錯誤的路徑或文件
最好以這種方式做
path='/home/quattro/file python /L9/files/'
def count_lines(path_to_file):
lines_counter = 0
with open(path_to_file, encoding='utf8') as f:
for i in f:
lines_counter += 1
return lines_counter
print(count_lines(path+'testo.txt'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.