Hướng dẫn how do you prompt the filename in python? - làm thế nào để bạn nhắc tên tệp trong python?

Chúng tôi thực sự không muốn phải chỉnh sửa mã Python mỗi khi chúng tôi muốn xử lý một tệp khác. Sẽ có thể sử dụng nhiều hơn khi yêu cầu người dùng nhập chuỗi tên tệp mỗi khi chương trình chạy để họ có thể sử dụng chương trình của chúng tôi trên các tệp khác nhau mà không thay đổi mã Python.

Điều này khá đơn giản để làm bằng cách đọc tên tệp từ người dùng bằng cách sử dụng

 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
1 như sau:

fname = input['Enter the file name: ']
fhand = open[fname]
count = 0
for line in fhand:
    if line.startswith['Subject:']:
        count = count + 1
fhand.close[]
print['There were', count, 'subject lines in', fname]

Chúng tôi đọc tên tệp từ người dùng và đặt nó vào một biến có tên

 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
2 và mở tệp đó. Bây giờ chúng ta có thể chạy chương trình nhiều lần trên các tệp khác nhau.

 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt

Đặt mã sau để mở và đếm các dòng của một tệp từ người dùng. Xem ra cho thụt lề và các khối mã bổ sung!

        count = 0
fname = input['Enter the file name: ']
---
fhand = open[fname]
---
for line in fhand:
---
for line in fname: #paired
---
    count = count + 1
---
fhand.close[]
print['There were', count, 'lines in', fname]
        

Trước khi nhìn trộm ở phần tiếp theo, hãy xem chương trình trên và tự hỏi mình, điều gì có thể đi ở đây? Hoặc những gì người dùng thân thiện của chúng ta có thể làm điều đó sẽ khiến chương trình nhỏ đẹp của chúng ta thoát ra một cách vô tình với một dấu vết, khiến chúng ta trông không quá ngầu trong mắt người dùng của chúng ta?

Có năm lỗi trong mã dưới đây. Sửa mã để yêu cầu người dùng cho một tệp, mở Mbox mbox-short.txt, và đếm các dòng bắt đầu bằng

 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
3.

Bạn đã thử các hoạt động trên trang này of activities on this page

Ví dụ 1: Sử dụng mô -đun HĐH

import os

# file name with extension
file_name = os.path.basename['/root/file.ext']

# file name without extension
print[os.path.splitext[file_name][0]]

Đầu ra

file

 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
4 Cho tên của tệp/thư mục cuối cùng của đường dẫn, trong khi
 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
5 chia tên tệp thành tên tệp và phần mở rộng.

import os

print[os.path.splitext[file_name]]

Đầu ra

['file', '.ext']

 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
4 Cho tên của tệp/thư mục cuối cùng của đường dẫn, trong khi
 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
5 chia tên tệp thành tên tệp và phần mở rộng.

from pathlib import Path

print[Path['/root/file.ext'].stem]

Đầu ra

file

 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
4 Cho tên của tệp/thư mục cuối cùng của đường dẫn, trong khi
 python search6.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt
5 chia tên tệp thành tên tệp và phần mở rộng.

Ví dụ 2: Sử dụng mô -đun đường dẫn

  1. Sử dụng thuộc tính
     python search6.py # seach6.py is the file containing the above script.
     Enter the file name: mbox.txt
     There were 1797 subject lines in mbox.txt
    
     python search6.py
      Enter the file name: mbox-short.txt
    There were 27 subject lines in mbox-short.txt
    
    6 của mô -đun
     python search6.py # seach6.py is the file containing the above script.
     Enter the file name: mbox.txt
     There were 1797 subject lines in mbox.txt
    
     python search6.py
      Enter the file name: mbox-short.txt
    There were 27 subject lines in mbox-short.txt
    
    7, tên tệp có thể được trích xuất như hình trên.
  2. Nó hoạt động cho Python 3,4 trở lên.
  • Cập nhật mới nhất
  • Lưu dưới dạng PDF

    Trang ID8611

    fname = input['Enter the file name: ']
    fhand = open[fname]
    count = 0
    for line in fhand:
        if line.startswith['Subject:']:
            count = count + 1
    print['There were', count, 'subject lines in', fname]
    
    # Code: //www.py4e.com/code3/search6.py

    Chúng tôi đọc tên tệp từ người dùng và đặt nó vào một biến có tên

     python search6.py # seach6.py is the file containing the above script.
     Enter the file name: mbox.txt
     There were 1797 subject lines in mbox.txt
    
     python search6.py
      Enter the file name: mbox-short.txt
    There were 27 subject lines in mbox-short.txt
    
    9 và mở tệp đó. Bây giờ chúng ta có thể chạy chương trình nhiều lần trên các tệp khác nhau.

     python search6.py # seach6.py is the file containing the above script.
     Enter the file name: mbox.txt
     There were 1797 subject lines in mbox.txt
    
     python search6.py
      Enter the file name: mbox-short.txt
    There were 27 subject lines in mbox-short.txt
    
    0

    Trước khi nhìn trộm ở phần tiếp theo, hãy xem chương trình trên và tự hỏi, "Điều gì có thể xảy ra ở đây?" hoặc "Người dùng thân thiện của chúng tôi có thể làm gì sẽ khiến chương trình nhỏ đẹp của chúng tôi thoát ra một cách vô tình với một dấu vết, khiến chúng tôi trông không quá ngầu trong mắt người dùng?"

    Bài Viết Liên Quan

    Chủ Đề