Hướng dẫn how do i save a file to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khá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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khá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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khác trong python?

Mehdi Nellen

7.9264 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.6K26 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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khá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

802K173 Huy hiệu vàng1724 Huy hiệu bạc1625 Huy hiệu Đồng173 gold badges1724 silver badges1625 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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khác trong python?

xrisk

3.66021 Huy hiệu bạc41 Huy hiệu đồng21 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

5094 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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khác trong 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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khác trong 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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khá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 to another folder in python? - làm cách nào để lưu một tệp vào một thư mục khá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

Làm cách nào để lưu một tệp từ thư mục này sang thư mục khác 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.

Làm thế nào để bạn sao chép một tệp từ thư mục sang thư mục khác trong Python?

Các bước dưới đây cho thấy cách sao chép một tệp từ thư mục này sang thư mục khác ...
Tìm đường dẫn của một tập tin.Chúng ta có thể sao chép một tệp bằng cả đường dẫn tương đối và đường dẫn tuyệt đối.....
Sử dụng hàm SOWL.Copy ().....
Sử dụng hàm Os.ListDir () và SOWLIL COPY () để sao chép tất cả các tệp.....
Sử dụng hàm copytree () để sao chép toàn bộ thư mục ..

Làm cách nào để lưu một tệp khác 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 để lưu tệp vào thư mục?

Các bước cần thiết để lưu tệp vào một vị trí tiêu chuẩn ...
Khởi chạy hộp thoại Lưu tệp.Trong menu Tệp, chọn mục Save As Menu ..
Đặt tên cho tập tin.Mở thư mục chứa tệp mong muốn.....
Chọn thư mục mong muốn để lưu tệp.....
Chỉ định loại định dạng tệp ..
Nhấp vào nút Lưu ..