25
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.
Bất cứ ai có thể hướng dẫn tôi làm thế nào tôi có thể nhận được đường dẫn tệp nếu chúng tôi chuyển tệp từ đối số dòng lệnh và trình trích xuất tệp. Trong trường hợp chúng ta cũng cần kiểm tra xem tệp có tồn tại thành thư mục cụ thể không
python.py /home/abhishek/test.txt
Nhận đường dẫn tệp và kiểm tra kiểm tra.txt tồn tại vào thư mục Abhishek.
Tôi biết nó có thể rất dễ dàng nhưng tôi có một chút mới với Pytho
hỏi ngày 16 tháng 1 năm 2013 lúc 14:14Jan 16, 2013 at 14:14
import os
import sys
fn = sys.argv[1]
if os.path.exists[fn]:
print os.path.basename[fn]
# file exists
Đã trả lời ngày 16 tháng 1 năm 2013 lúc 14:15Jan 16, 2013 at 14:15
Eumiroeumiroeumiro
199K34 Huy hiệu vàng294 Huy hiệu bạc259 Huy hiệu Đồng34 gold badges294 silver badges259 bronze badges
2
Bắt đầu với Python 3.4, bạn có thể sử dụng argparse cùng với pathlib:
import argparse
from pathlib import Path
parser = argparse.ArgumentParser[]
parser.add_argument["file_path", type=Path]
p = parser.parse_args[]
print[p.file_path, type[p.file_path], p.file_path.exists[]]
Đã trả lời ngày 23 tháng 5 năm 2019 lúc 9:00May 23, 2019 at 9:00
Nijmnijmnijm
2.11712 huy hiệu bạc28 Huy hiệu đồng12 silver badges28 bronze badges
Tôi nghĩ rằng cách thanh lịch nhất là sử dụng ArgumentParser
theo cách này, bạn thậm chí còn nhận được tùy chọn -h
giúp người dùng tìm ra cách vượt qua các đối số. Tôi cũng đã bao gồm một đối số tùy chọn [--outputDirectory
].
Bây giờ bạn có thể chỉ cần thực thi với python3 test.py /home/test.txt --outputDirectory /home/testDir/
import argparse
import sys
import os
def create_arg_parser[]:
# Creates and returns the ArgumentParser object
parser = argparse.ArgumentParser[description='Description of your app.']
parser.add_argument['inputDirectory',
help='Path to the input directory.']
parser.add_argument['--outputDirectory',
help='Path to the output that contains the resumes.']
return parser
if __name__ == "__main__":
arg_parser = create_arg_parser[]
parsed_args = arg_parser.parse_args[sys.argv[1:]]
if os.path.exists[parsed_args.inputDirectory]:
print["File exist"]
Đã trả lời ngày 16 tháng 11 năm 2017 lúc 7:50Nov 16, 2017 at 7:50
Ph03n1xPh03n1xPh03n1x
7646 Huy hiệu bạc12 Huy hiệu Đồng6 silver badges12 bronze badges
Dùng cái này:
import sys
import os
path = sys.argv[1]
# Check if path exits
if os.path.exists[path]:
print "File exist"
# Get filename
print "filename : " + path.split["/"][-1]
Đã trả lời ngày 16 tháng 1 năm 2013 lúc 14:17Jan 16, 2013 at 14:17
AtoztoaatoztoaATOzTOA
33.6K22 Huy hiệu vàng93 Huy hiệu bạc116 Huy hiệu đồng22 gold badges93 silver badges116 bronze badges
Kiểm tra câu trả lờiBest Answer
Làm thế nào bạn nhập đường dẫn tệp vào dấu nhắc lệnh? & nbsp; mã của bạn hoạt động rất tốt cho tôi, vì vậy tôi nghĩ có lẽ đó là cách bạn chạy nó; Tất cả các công cụ Argparse là tốt. & nbsp; đây là những gì tôi có chính xác:
Python
# As you can see, this is pretty much identical to your code from argparse import ArgumentParser parser = ArgumentParser[] parser.add_argument["-f", "--file", dest="myFile", help="Open specified file"] args = parser.parse_args[] myFile = args.myFile # This is the one part I added [the read[] call] text = open[myFile] print[text.read[]] # Then in the command prompt I navigated to the script folder and did it like this: # my_script.py -f "C:\path\to\my\file to read.py"
Hy vọng điều đó sẽ giúp.
Bài đăng này có hữu ích không? Thumb_Up Thumb_down thumb_up thumb_down