Hướng dẫn can you write to a .py file in python? - bạn có thể ghi vào tệp .py trong python không?

4

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi chỉ đang loay hoay với ý tưởng viết một tệp .py bằng tập lệnh Python của tôi và sau đó nhập nó vào một tập lệnh anothe cho mục đích đọc.

Đây là mã:

f=open(filename,'w')

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
1 và sau đó tôi đang nhập tệp trong một tập lệnh khác và cho tôi lỗi:

TypeError: Chuỗi mã nguồn không thể chứa null byte

Bất kỳ ý tưởng về làm thế nào để tôi sửa chữa điều này?

Thanks.

Hướng dẫn can you write to a .py file in python? - bạn có thể ghi vào tệp .py trong python không?

Bigfoot11

9012 Huy hiệu vàng10 Huy hiệu bạc25 Huy hiệu đồng2 gold badges10 silver badges25 bronze badges

Đã hỏi ngày 8 tháng 3 năm 2015 lúc 11:56Mar 8, 2015 at 11:56

1

Nó nên hoạt động về nguyên tắc. Bạn thậm chí có thể nhập nó vào cùng một kịch bản. Vấn đề có lẽ là với

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
2. Khi bạn thử ví dụ (đầy đủ) sau đây ...

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)

Nó sẽ in ...

[5]

Đã trả lời ngày 8 tháng 3 năm 2015 lúc 22:01Mar 8, 2015 at 22:01


Ghi vào một tệp hiện có

Để ghi vào một tệp hiện có, bạn phải thêm một tham số vào hàm

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
3:

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
4 - Phụ lục - sẽ nối vào cuối tệp

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
5 - Viết - sẽ ghi đè lên bất kỳ nội dung hiện có nào

Thí dụ

Mở tệp "demofile2.txt" và nối nội dung vào tệp:

f = open ("demofile2.txt", "a") f.write ("Bây giờ tệp có nhiều nội dung hơn!") f.close ()
f.write("Now the file has more content!")
f.close()

#Open và đọc tệp sau khi thêm: f = open ("demofile2.txt", "r") in (f.Read ())
f = open("demofile2.txt", "r")
print(f.read())

Chạy ví dụ »

Thí dụ

Mở tệp "demofile2.txt" và nối nội dung vào tệp:

f = open ("demofile2.txt", "a") f.write ("Bây giờ tệp có nhiều nội dung hơn!") f.close ()
f.write("Woops! I have deleted the content!")
f.close()

#Open và đọc tệp sau khi thêm: f = open ("demofile2.txt", "r") in (f.Read ())
f = open("demofile3.txt", "r")
print(f.read())

Chạy ví dụ »

Mở tệp "demofile3.txt" và ghi đè nội dung: the "w" method will overwrite the entire file.


f = open ("demofile3.txt", "w") f.write ("woops! Tôi đã xóa nội dung!") f.close ()

#open và đọc tệp sau khi thêm: f = open ("demofile3.txt", "r") in (f.Read ())

Lưu ý: Phương thức "W" sẽ ghi đè lên toàn bộ tệp.

Tạo một tệp mới

Để tạo một tệp mới trong Python, hãy sử dụng phương thức

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
3, với một trong các tham số sau:

Thí dụ

Mở tệp "demofile2.txt" và nối nội dung vào tệp:

f = open ("demofile2.txt", "a") f.write ("Bây giờ tệp có nhiều nội dung hơn!") f.close ()

#Open và đọc tệp sau khi thêm: f = open ("demofile2.txt", "r") in (f.Read ())

Thí dụ

Mở tệp "demofile2.txt" và nối nội dung vào tệp:

f = open ("demofile2.txt", "a") f.write ("Bây giờ tệp có nhiều nội dung hơn!") f.close ()



Python cung cấp các chức năng sẵn có để tạo, viết và đọc các tệp. Có hai loại tệp có thể được xử lý trong Python, tệp văn bản thông thường và tệp nhị phân (được viết bằng ngôn ngữ nhị phân, 0s và 1s).

  • Tệp văn bản: Trong loại tệp này, mỗi dòng văn bản được chấm dứt với một ký tự đặc biệt có tên EOL (cuối dòng), là ký tự dòng mới (‘\ n,) trong Python theo mặc định. In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n’) in python by default.
  • Tệp nhị phân: Trong loại tệp này, không có bộ hủy nào cho một dòng và dữ liệu được lưu trữ sau khi chuyển đổi nó thành ngôn ngữ nhị phân có thể hiểu bằng máy. In this type of file, there is no terminator for a line and the data is stored after converting it into machine-understandable binary language.

Lưu ý: Để biết thêm về xử lý tệp bấm vào đây. To know more about file handling click here.

Mục lục

  • Chế độ truy cập
  • Mở một tập tin
  • Đóng một tập tin
  • Viết vào tệp
    • Nối vào một tập tin
    • Với tuyên bố

Chế độ truy cập

Mở một tập tin

  1. Đóng một tập tin Open the file for writing. For an existing file, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
  2. Viết vào tệp Open the file for reading and writing. For an existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
  3. Nối vào một tập tin Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.

Với tuyên bố To know more about access mode click here.

Các chế độ truy cập chi phối loại hoạt động có thể trong tệp đã mở. Nó đề cập đến cách các tập tin sẽ được sử dụng sau khi nó mở. Các chế độ này cũng xác định vị trí của xử lý tệp trong tệp. Xử lý tệp giống như một con trỏ, xác định từ nơi dữ liệu phải được đọc hoặc ghi trong tệp. Các chế độ truy cập khác nhau để đọc tệp là -

Chỉ viết (‘W,): Mở tệp để viết. Đối với một tệp hiện có, dữ liệu bị cắt cụt và viết quá mức. Tay cầm được định vị ở đầu tệp. Tạo tệp nếu tệp không tồn tại.

Syntax:

File_object = open(r"File_Name", "Access_Mode")

Viết và đọc (‘W+,): Mở tệp để đọc và viết. Đối với một tệp hiện có, dữ liệu bị cắt ngắn và viết quá mức. Tay cầm được định vị ở đầu tệp.

Chỉ nối thêm (‘A,): Mở tệp để viết. Tệp được tạo nếu nó không tồn tại. Tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có. The

[5]
1 is placed before filename to prevent the characters in filename string to be treated as special character. For example, if there is \temp in the file address, then \t is treated as the tab character and error is raised of invalid address. The r makes the string raw, that is, it tells that the string is without any special characters. The r can be ignored if the file is in same directory and address is not being placed.

Lưu ý: Để biết thêm về chế độ truy cập bấm vào đây.

Mở một tập tin

Nó được thực hiện bằng cách sử dụng hàm ____2020. Không có mô -đun nào được yêu cầu nhập khẩu cho chức năng này.

Đóng một tập tin

Viết vào tệp

Syntax:

File_object.close()

Lưu ý: Để biết thêm về chế độ truy cập bấm vào đây.

File_object.close()
7

Viết vào tệp

Nối vào một tập tin

  1. Với tuyên bố Inserts the string str1 in a single line in the text file.
    File_object.write(str1)
    
  2. Các chế độ truy cập chi phối loại hoạt động có thể trong tệp đã mở. Nó đề cập đến cách các tập tin sẽ được sử dụng sau khi nó mở. Các chế độ này cũng xác định vị trí của xử lý tệp trong tệp. Xử lý tệp giống như một con trỏ, xác định từ nơi dữ liệu phải được đọc hoặc ghi trong tệp. Các chế độ truy cập khác nhau để đọc tệp là - For a list of string elements, each string is inserted in the text file. Used to insert multiple strings at a single time.
    File_object.writelines(L) for L = [str1, str2, str3] 
    

Chỉ viết (‘W,): Mở tệp để viết. Đối với một tệp hiện có, dữ liệu bị cắt cụt và viết quá mức. Tay cầm được định vị ở đầu tệp. Tạo tệp nếu tệp không tồn tại.

File_object.close()
8 is treated as a special character of two bytes.

Example:

Viết và đọc (‘W+,): Mở tệp để đọc và viết. Đối với một tệp hiện có, dữ liệu bị cắt ngắn và viết quá mức. Tay cầm được định vị ở đầu tệp.

Chỉ nối thêm (‘A,): Mở tệp để viết. Tệp được tạo nếu nó không tồn tại. Tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có.

Lưu ý: Để biết thêm về chế độ truy cập bấm vào đây.

File_object.writelines(L) for L = [str1, str2, str3] 
9

Hello
This is Delhi
This is Paris
This is London
0

Hello
This is Delhi
This is Paris
This is London
1

Mở một tập tin

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
1

Hello
This is Delhi
This is Paris
This is London
1

Output:

Hello
This is Delhi
This is Paris
This is London

Nối vào một tập tin

Khi tệp được mở ở chế độ nối, tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có. Hãy cùng xem ví dụ dưới đây để làm rõ sự khác biệt giữa chế độ ghi và chế độ nối.

[5]
2
[5]
3
[5]
4
[5]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7
[5]
7
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
5
File_object.write(str1)
6

File_object.write(str1)
7
[5]
3
File_object.write(str1)
9
File_object.writelines(L) for L = [str1, str2, str3] 
0
[5]
7
File_object.writelines(L) for L = [str1, str2, str3] 
2
[5]
7__6444565

Hello
This is Delhi
This is Paris
This is London
0

Hello
This is Delhi
This is Paris
This is London
1

Các

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
10
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
11
File_object.write(str1)
6

Hello
This is Delhi
This is Paris
This is London
1

[5]
2
[5]
3
[5]
4
[5]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7
[5]
7
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
20
File_object.write(str1)
6

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
[5]
5
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
24
File_object.write(str1)
6

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
1

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
29

Hello
This is Delhi
This is Paris
This is London
1

[5]
2
[5]
3
[5]
4
[5]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7
[5]
7
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
5
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
09

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
10
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
40
File_object.write(str1)
6

Hello
This is Delhi
This is Paris
This is London
1

[5]
2
[5]
3
[5]
4
[5]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7
[5]
7
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
20
File_object.write(str1)
6

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
[5]
5
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
53
File_object.write(str1)
6

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
1

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
29

Hello
This is Delhi
This is Paris
This is London
1

Output:

Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow

[5]
2
[5]
3
[5]
4
[5]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7
[5]
7
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
5
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
09

Với tuyên bố

Syntax:

with open filename as file:

File_object.write(str1)
7
[5]
3
File_object.write(str1)
9
File_object.writelines(L) for L = [str1, str2, str3] 
0
[5]
7
File_object.writelines(L) for L = [str1, str2, str3] 
2
[5]
7__6444565

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
60
[5]
4
[5]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7
[5]
7
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
5
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
78

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
79
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
10
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
81
File_object.write(str1)
6

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
79
Hello
This is Delhi
This is Paris
This is London
0

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
60
[5]
4
[5]
5
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
7
[5]
7
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
90
#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
78

#!python3

fname = 'generated.py'
data = 5

with open(fname, 'w') as f:
    f.write('data = [{}]'.format(data))

import generated
print(generated.data)
79
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
0
Output of Readlines after appending
This is Delhi
This is Paris
This is London
Today


Output of Readlines after writing
Tomorrow
1

Output:

Hello
This is Delhi
This is Paris
This is London

Các To know more about with statement click here.


Bạn có thể viết cho một tập tin Python trong Python không?

Viết vào các tệp trong Python để ghi vào một tệp trong Python, chúng ta cần mở nó trong Write W, nối thêm chế độ Creation X hoặc độc quyền. Chúng ta cần cẩn thận với chế độ W, vì nó sẽ ghi đè vào tệp nếu nó đã tồn tại.In order to write into a file in Python, we need to open it in write w , append a or exclusive creation x mode. We need to be careful with the w mode, as it will overwrite into the file if it already exists.

Làm cách nào để ghi vào một tập tin py?

Bạn có thể ghi vào một tệp trong Python bằng hàm Open ().Bạn phải chỉ định một trong hai W W W hoặc một tham số để ghi vào một tệp.Càng W W Werwer ghi lại nội dung hiện có của một tệp.Một số người khác nối nội dung vào một tập tin.using the open() function. You must specify either “w” or “a” as a parameter to write to a file. “w” overwrites the existing content of a file. “a” appends content to a file.

Bạn có thể viết kịch bản trong Python không?

Viết tập lệnh đầu tiên của bạn, một tập lệnh trong Python gần giống hệt như tập lệnh shell bạn đã gặp, tức là một tệp văn bản đơn giản chứa các dòng mã python sẽ được thực thi lần lượt.Để tạo và chỉnh sửa tập lệnh Python, việc sử dụng trình soạn thảo văn bản có cú pháp làm nổi bật.A script in python is almost exactly the same as a Shell script you've already met, i.e. it is a plain text file that contains lines of Python code that will be executed one after another. To create and edit a Python script, it is almost essential to use a text editor with syntax highlighting.

Là một tệp .py chỉ là một tệp văn bản?

Tệp PY là một tệp chương trình hoặc tập lệnh được viết bằng Python, một ngôn ngữ lập trình hướng đối tượng được diễn giải.Nó có thể được tạo và chỉnh sửa với một trình soạn thảo văn bản, nhưng yêu cầu một trình thông dịch Python để chạy.Các tệp PY thường được sử dụng để lập trình máy chủ web và các hệ thống máy tính hành chính khác.. It can be created and edited with a text editor, but requires a Python interpreter to run. PY files are often used to program web servers and other administrative computer systems.