Hướng dẫn how do i save a file in python path? - làm cách nào để lưu một tệp trong đường dẫn python?

90

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.

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 python path? - làm cách nào để lưu một tệp trong đường dẫn 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 python path? - làm cách nào để lưu một tệp trong đường dẫn python?

Mehdi Nellen

7.8964 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.5K26 Huy hiệu vàng129 Huy hiệu bạc171 Huy hiệu đồng26 gold badges129 silver badges171 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 python path? - làm cách nào để lưu một tệp trong đường dẫn python?

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

UnutbuUnutbuunutbu

800K172 Huy hiệu vàng1724 Huy hiệu bạc1624 Huy hiệu đồng172 gold badges1724 silver badges1624 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 python path? - làm cách nào để lưu một tệp trong đường dẫn python?

xrisk

3.63020 Huy hiệu bạc41 Huy hiệu đồng20 silver badges41 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

5074 Huy hiệu bạc15 Huy hiệu Đồng4 silver badges15 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 python path? - làm cách nào để lưu một tệp trong đường dẫn python?

Paul Thomaspaul ThomasPaul Thomas

4677 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 python path? - làm cách nào để lưu một tệp trong đường dẫn python?

AsherasherAsher

2.5186 Huy hiệu vàng26 Huy hiệu bạc40 Huy hiệu đồng6 gold badges26 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 python path? - làm cách nào để lưu một tệp trong đường dẫn 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 python path? - làm cách nào để lưu một tệp trong đường dẫn 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

Làm cách nào để lưu một đường dẫn tệp?

Nhấp vào nút Bắt đầu và sau đó nhấp vào Máy tính, nhấp để mở vị trí của thư mục mong muốn, sau đó nhấp chuột phải vào bên phải của đường dẫn trong thanh địa chỉ.Địa chỉ sao chép: Nhấp vào tùy chọn này để lưu vị trí theo định dạng được tối ưu hóa để sao chép và dán trong Windows Explorer.right-click to the right of the path in the address bar. Copy Address: Click this option to save the location in a format that is optimized for copying and pasting in Windows Explorer.

Làm thế nào để bạn lưu tệp trong Python?

Nhấp chuột phải vào cửa sổ Python và chọn Lưu AS để lưu mã của bạn dưới dạng tệp Python (. PY) hoặc Tệp văn bản (. TXT).Nếu lưu vào tệp Python, chỉ có mã Python sẽ được lưu. to save your code either as a Python file (. py) or Text file (. txt). If saving to a Python file, only the Python code will be saved.

Làm cách nào để di chuyển một tập tin đến một đường dẫn trong Python?

Di chuyển () Phương thức di chuyển các tệp trong Python bằng cách sử dụng.Shutil.Phương thức di chuyển () lấy hai đối số đầu tiên là đường dẫn nguồn hoàn chỉnh và hướng thứ hai là đường dẫn đích (bao gồm tên tệp/thư mục để di chuyển), hàm di chuyển sẽ chuyển tệp từ nguồn sang đích. move Files in Python using the. The shutil. move() method takes two arguments first one is the complete source path and the second one is the destination path (including the file/folder name to move), the move function will move the file from source to the destination.