Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

Làm thế nào để bạn nói với Python nơi lưu tệp văn bản?

Ví dụ: máy tính của tôi đang chạy tệp Python khỏi máy tính để bàn của tôi. Tôi muốn nó lưu tất cả các tệp văn bản trong thư mục tài liệu của tôi, không phải trên máy tính để bàn của tôi. Làm thế nào để tôi làm điều đó trong một kịch bản như thế này?

name_of_file = raw_input("What is the name of the file: ")
completeName = name_of_file + ".txt"
#Alter this line in any shape or form it is up to you.
file1 = open(completeName , "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

Đã hỏi ngày 6 tháng 11 năm 2011 lúc 0:01Nov 6, 2011 at 0:01

Chỉ cần sử dụng một đường dẫn tuyệt đối khi mở FileHandle để viết.

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()

Bạn có thể tùy chọn kết hợp điều này với

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
2 như được mô tả trong câu trả lời của Bryan để tự động lấy đường dẫn của thư mục tài liệu của người dùng. Chúc mừng!

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

Mehdi Nellen

8.0264 Huy hiệu vàng32 Huy hiệu bạc48 Huy hiệu đồng4 gold badges32 silver badges48 bronze badges

Đã trả lời ngày 6 tháng 11 năm 2011 lúc 0:02Nov 6, 2011 at 0:02

AcornacornAcorn

47.7K26 Huy hiệu vàng129 Huy hiệu bạc172 Huy hiệu đồng26 gold badges129 silver badges172 bronze badges

0

Sử dụng OS.Path.Join để kết hợp đường dẫn đến thư mục

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
3 với
import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
4 (tên tệp?) Được cung cấp bởi người dùng.

import os
with open(os.path.join('/path/to/Documents',completeName), "w") as file1:
    toFile = raw_input("Write what you want into the field")
    file1.write(toFile)

Nếu bạn muốn thư mục

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
3 có liên quan đến thư mục nhà của người dùng, bạn có thể sử dụng một cái gì đó như:

os.path.join(os.path.expanduser('~'),'Documents',completeName)

Những người khác đã đề xuất sử dụng

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
6. Lưu ý rằng
import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
6 không giải quyết
import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
8 cho thư mục nhà của người dùng:

In [10]: cd /tmp
/tmp

In [11]: os.path.abspath("~")
Out[11]: '/tmp/~'

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

Đã trả lời ngày 6 tháng 11 năm 2011 lúc 0:04Nov 6, 2011 at 0:04

UnutbuUnutbuunutbu

806K173 Huy hiệu vàng1735 Huy hiệu bạc1633 Huy hiệu Đồng173 gold badges1735 silver badges1633 bronze badges

Một bản cập nhật nhỏ cho điều này.

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
9 được đổi tên thành
import os
with open(os.path.join('/path/to/Documents',completeName), "w") as file1:
    toFile = raw_input("Write what you want into the field")
    file1.write(toFile)
0 trong Python 3.

Ghi chú phát hành Python 3

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

xrisk

3.70021 huy hiệu bạc43 huy hiệu đồng21 silver badges43 bronze badges

Đã trả lời ngày 20 tháng 9 năm 2018 lúc 12:26Sep 20, 2018 at 12:26

der_radlerder_radlerder_radler

5094 Huy hiệu bạc16 Huy hiệu Đồng4 silver badges16 bronze badges

Một cách đơn giản khác mà không sử dụng hệ điều hành nhập khẩu là,

outFileName="F:\\folder\\folder\\filename.txt"
outFile=open(outFileName, "w")
outFile.write("""Hello my name is ABCD""")
outFile.close()

Đã trả lời ngày 30 tháng 12 năm 2018 lúc 1:48Dec 30, 2018 at 1:48

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

Paul Thomaspaul ThomasPaul Thomas

4777 Huy hiệu bạc15 Huy hiệu Đồng7 silver badges15 bronze badges

Nếu bạn muốn lưu một tệp vào một thư mục cụ thể và tên tệp thì đây là một số ví dụ đơn giản. Nó cũng kiểm tra xem thư mục có hoặc chưa được tạo không.

import os.path
directory = './html/'
filename = "file.html"
file_path = os.path.join(directory, filename)
if not os.path.isdir(directory):
    os.mkdir(directory)
file = open(file_path, "w")
file.write(html)
file.close()

Hy vọng điều này sẽ giúp bạn!

Đã trả lời ngày 3 tháng 12 năm 2019 lúc 3:45Dec 3, 2019 at 3:45

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

AsherasherAsher

2.5186 huy hiệu vàng27 Huy hiệu bạc40 Huy hiệu đồng6 gold badges27 silver badges40 bronze badges

Sử dụng một chuỗi tuyệt đối hoặc tương đối làm tên tệp.

name_of_file = input("What is the name of the file: ")
completeName = '/home/user/Documents'+ name_of_file + ".txt"
file1 = open(completeName , "w")
toFile = input("Write what you want into the field")
file1.write(toFile)
file1.close()

Đã trả lời ngày 11 tháng 10 năm 2018 lúc 10:20Oct 11, 2018 at 10:20

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

1

Chỉ cần cung cấp đường dẫn mong muốn của bạn nếu tệp không tồn tại sớm hơn;

        from os.path import abspath
        with open ('C:\\Users\\Admin\\Desktop\\results.txt', mode = 'w') as final1:
            print(final1.write('This is my new file.'))

        print(f'Text has been processed and saved at {abspath(final1.name)}')

Đầu ra sẽ là:

Text has been processed and saved at C:\Users\Admin\Desktop\results.txt

Đã trả lời ngày 30 tháng 6 năm 2020 lúc 4:03Jun 30, 2020 at 4:03

Hướng dẫn how do i save a file in the same directory in python? - làm cách nào để lưu tệp trong cùng thư mục trong python?

Nếu bạn muốn lưu tệp hoặc một số khác trong thư mục, bạn có thể sử dụng mô -đun

import os
with open(os.path.join('/path/to/Documents',completeName), "w") as file1:
    toFile = raw_input("Write what you want into the field")
    file1.write(toFile)
1 từ thư viện Python cơ sở.

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
0

Bạn có thể làm điều đó cho một số danh sách của một số tệp hoặc trong các truy vấn Django:

import os.path

save_path = 'C:/example/'

name_of_file = raw_input("What is the name of the file: ")

completeName = os.path.join(save_path, name_of_file+".txt")         

file1 = open(completeName, "w")

toFile = raw_input("Write what you want into the field")

file1.write(toFile)

file1.close()
1

Tất cả các tập tin được chọn của bạn sẽ được đặt trong thư mục có hướng.

Đã trả lời ngày 17 tháng 3 năm 2021 lúc 15:52Mar 17, 2021 at 15:52