Hướng dẫn how to take input from another file in python - cách lấy dữ liệu đầu vào từ một tệp khác trong python

Các bình luận là đúng, nhưng có vẻ như bạn muốn biết. Vấn đề đã được nhấn mạnh bởi @martineau. Điều gì đang xảy ra là như sau:

  1. Test1.py thực thi trong quy trình riêng của mình và mở tệp zip.
  2. test1.py yêu cầu hệ điều hành bắt đầu test2.pypy
  3. test2.py thực hiện trong quá trình riêng của mình và không có khái niệm về tệp zip tồn tại trong quá trình con của test1.py

Bởi vì họ không biết về nhau, tùy chọn dễ nhất bạn có theo cách bạn muốn thực hiện (tức là sử dụng Os.System ()), là truyền thông tin từ Test1.py sang Test2. sợi dây. Một cách sẽ là như sau:

test1.py::

import zipfile
import os

inputzip = input("Enter the Zip File:")
files = zipfile.ZipFile(inputzip, "r")
name_list = (" ".join([i for i in files.namelist()]))
os.system("python3 test2.py {0}".format(name_list))

test2.py::

import zipfile
import sys

print ("The list of files in the zip : ")

for name in sys.argv[1:]:
   print (name)

output::

Enter the Zip File:test.zip
The list of files in the zip : 
test
txt1
txt2

Cuối cùng, điều này khá lộn xộn và như đã chỉ ra bởi @joelgoldstick, bạn chỉ nên nhập phương thức từ test2.py vào test1.py. Ví dụ:

test2.py::

def test2_method(zipfile):
  print ("The list of files in the zip : ")

  for name in zipfile.namelist():
     print (name)

test1.py::

import zipfile

from test2 import test2_method

inputzip = input("Enter the Zip File:")
files = zipfile.ZipFile(inputzip, "r")

test2_method(files)

output::

Enter the Zip File:test.zip
The list of files in the zip : 
test
txt1
txt2

Tôi hiện đang tự hỏi làm thế nào dễ dàng để có cả hai quy trình chia sẻ bộ nhớ và một quy trình để có thể truyền tham chiếu bộ nhớ của đối tượng tệp zip đã mở cho cái kia. Không biết liệu điều này thậm chí có thể không> _

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Khi các dòng mã tăng lên, thật cồng kềnh khi tìm kiếm khối mã cần thiết. Đó là một thực tế tốt để phân biệt các dòng mã theo công việc của họ. Nó có thể được thực hiện bằng cách có các tệp riêng biệt cho các mã làm việc khác nhau. Như chúng ta đã biết, các thư viện khác nhau trong Python cung cấp các phương thức và biến khác nhau mà chúng ta truy cập bằng cách sử dụng nhập đơn giản. Ví dụ, thư viện toán học. Nếu chúng tôi muốn sử dụng biến PI, chúng tôi sử dụng nhập khẩu nhập khẩu và sau đó là Math.pi.

    Để nhập các biến từ một tệp khác, chúng tôi phải nhập tệp đó từ chương trình hiện tại. Điều này sẽ cung cấp quyền truy cập vào tất cả các phương thức và biến có sẵn trong tệp đó.

    Tuyên bố nhập khẩu

    Chúng tôi có thể sử dụng bất kỳ tệp nguồn Python nào làm mô -đun bằng cách thực thi câu lệnh nhập trong một số tệp nguồn Python khác. Khi trình thông dịch gặp một câu lệnh nhập, nó sẽ nhập mô -đun nếu mô -đun có mặt trong đường dẫn tìm kiếm. Đường dẫn tìm kiếm là danh sách các thư mục mà thông dịch viên tìm kiếm để nhập mô -đun.

    Tuyên bố từ nhập khẩuFor more information, refer Python Modules

    Python từ câu lệnh cho phép bạn nhập các thuộc tính cụ thể từ một mô -đun. & NBSP;approaches to import variables from other file

    • Lưu ý: Để biết thêm thông tin, hãy tham khảo các mô -đun Python
    • Các cách tiếp cận khác nhau để nhập các biến từ tệp khác
    • Nhập và sau đó sử dụng. để truy cập biến

    Example:

    từ nhập và sử dụng các biến

    Python3

    từ nhập * và sau đó sử dụng các biến trực tiếp.

    Giả sử chúng ta có một tệp có tên là hoán đổi. Chúng tôi phải nhập biến X và Y từ tệp này trong một tệp khác có tên là Cal Calval.py. & NBSP;

    x = 23

    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    0=
    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    2

    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    3
    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    4

    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    5
    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    6=
    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    8

    Python3

    import zipfile
    import sys
    
    print ("The list of files in the zip : ")
    
    for name in sys.argv[1:]:
       print (name)
    
    5
    Enter the Zip File:test.zip
    The list of files in the zip : 
    test
    txt1
    txt2
    
    0
    Enter the Zip File:test.zip
    The list of files in the zip : 
    test
    txt1
    txt2
    
    1

    Bây giờ, hãy tạo tệp Python thứ hai để gọi biến từ mã trên:

    Enter the Zip File:test.zip
    The list of files in the zip : 
    test
    txt1
    txt2
    
    2
    Enter the Zip File:test.zip
    The list of files in the zip : 
    test
    txt1
    txt2
    
    3

    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    0
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    1
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    2
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    3
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    4
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    5

    Enter the Zip File:test.zip
    The list of files in the zip : 
    test
    txt1
    txt2
    
    4=
    Enter the Zip File:test.zip
    The list of files in the zip : 
    test
    txt1
    txt2
    
    6

    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    0
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    1
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    2
    import zipfile
    
    from test2 import test2_method
    
    inputzip = input("Enter the Zip File:")
    files = zipfile.ZipFile(inputzip, "r")
    
    test2_method(files)
    
    2
    def test2_method(zipfile):
      print ("The list of files in the zip : ")
    
      for name in zipfile.namelist():
         print (name)
    
    4
    import zipfile
    
    from test2 import test2_method
    
    inputzip = input("Enter the Zip File:")
    files = zipfile.ZipFile(inputzip, "r")
    
    test2_method(files)
    
    4

    Output:

    x value:  23 y value: 30
    x value:  30 y value: 23

    Làm thế nào để bạn lấy dữ liệu từ một tệp khác trong Python?

    Để nhập các biến từ một tệp khác, chúng tôi phải nhập tệp đó từ chương trình hiện tại ...
    Nhập và sau đó sử dụng.để truy cập biến ..
    từ nhập và sử dụng các biến ..
    từ nhập * và sau đó sử dụng các biến trực tiếp ..

    Tôi có thể nhập một biến từ một tệp python khác không?

    Làm cách nào để nhập một biến từ tệp này sang tệp khác trong Python?Nhập và sau đó sử dụng.Để truy cập biến. Từ nhập và sử dụng các biến.import and then use . to access variable. from import and use variables.

    Làm thế nào để bạn lấy một đầu vào trong Python?

    Trong Python, chúng tôi sử dụng hàm input () để lấy đầu vào từ người dùng.Bất cứ điều gì bạn nhập làm đầu vào, hàm đầu vào chuyển đổi nó thành một chuỗi.Nếu bạn nhập hàm INPUT () giá trị số nguyên, hãy chuyển đổi nó thành một chuỗi.use input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function convert it into a string.

    Làm thế nào để bạn viết đầu vào vào một tệp trong Python?

    Lưu đầu vào của người dùng vào một tệp trong Python #..
    Sử dụng câu lệnh With Open () để mở tệp ở chế độ ghi ..
    Sử dụng hàm input () để lấy đầu vào từ người dùng ..
    Sử dụng tệp.Viết () phương thức để ghi đầu vào vào tệp ..
    Câu lệnh với câu lệnh tự động đóng tệp ..