Hướng dẫn does python use backslash or forward slash? - python sử dụng dấu gạch chéo ngược hay dấu gạch chéo ngược?

Tôi có một quy trình

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
8S một thư mục và các thư mục con của nó để lọc các tệp PDF, tách tên của chúng và các đường dẫn tương ứng. Vấn đề tôi gặp phải là nó sẽ quét thư mục hàng đầu và in tên tệp thích hợp, ví dụ:
def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
9 Nhưng thứ hai nó quét một thư mục con, ví dụ
C:\folderA\folderB to C:/folderA/folderB
0

G:/Books/Sub Folder\\Title.Pdf

(Rõ ràng là một tên đường dẫn không hợp lệ). Nó cũng sẽ thêm \\ vào bất kỳ thư mục con nào trong các thư mục con.

Dưới đây là thủ tục:

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address

Tôi biết đó là một cái gì đó đơn giản mà tôi đang thiếu nhưng vì tình yêu cũng như tiền bạc, tôi có thể thấy nó là gì. Một đôi mắt mới sẽ giúp rất nhiều!

Tôi đã có ngày đầu tiên đầy đủ đầu tiên của tôi để học Python hôm nay, làm việc trong Visual Studio Code. Tôi vô cùng hạnh phúc. Bây giờ tôi có thể mở một hộp thoại để chọn một tệp, sử dụng hộp thoại để chọn một nơi để tạo thư mục và sao chép một tệp từ nơi này sang nơi khác. Tôi sẽ nói rằng dự án đầu tiên của tôi đã hoàn thành 25%.

Bây giờ tôi đang học cách làm việc với các tệp CSV bằng cách sử dụng gấu trúc. Theo một hướng dẫn vài tuần trước, tôi có thể dễ dàng mở các tệp CSV, chọn các cột mà tôi muốn giữ và lưu đầu ra. Bây giờ bắt đầu mới, tôi không thể lấy tập lệnh của mình để xem tệp CSV được lưu trong cùng một thư mục. Tôi quyết định xem liệu tôi có thể mã hóa đường dẫn như một bước đệm không, và điều đó đã cho tôi tất cả các lỗi giống nhau, cho đến khi xem một số hướng dẫn không liên quan, tôi thấy một người đảo ngược các dấu gạch chéo từ đường dẫn mà con đường đến tập tin của anh ta, để đến Tiền đạo chém. (Bằng cách nào đó anh ta thay thế khoảng 3 bằng một lần nhấn phím, nhưng đó là một câu hỏi thứ yếu). Tôi mệt mỏi và nó hiển thị CSV của tôi tốt mà không có lỗi.

Làm thế nào để tôi có được kịch bản của mình để đối phó với toàn bộ cửa sổ này là kỳ lạ và đã đảo ngược những vết chém từ những người khác?

Tôi thậm chí không biết Google gì, tôi cho rằng có một thư viện mà tôi nên sử dụng nhưng tôi không biết nó là gì.

Tôi đang làm việc trong Python và tôi cần chuyển đổi điều này:

Nội phân chính

  • Làm thế nào để bạn gõ một chém về phía trước trong Python?
  • Làm thế nào để bạn thay đổi chém lùi để chuyển tiếp về phía trước?
  • Làm thế nào để bạn thay đổi dấu gạch chéo ngược thành một chuỗi trong Python?
  • Làm thế nào để bạn chém vào Python?

C:\folderA\folderB to C:/folderA/folderB

Tôi có ba cách tiếp cận:

dir = s.replace('\\','/')

dir = os.path.normpath(s) 

dir = os.path.normcase(s)

Trong mỗi kịch bản, đầu ra đã được

C:folderAfolderB

Tôi không chắc mình đang làm gì sai, bất kỳ đề xuất nào?

Martineau

Huy hiệu vàng 115K2525 gold badges160 silver badges283 bronze badges

Hỏi ngày 5 tháng 8 năm 2014 lúc 19:39Aug 5, 2014 at 19:39

3

Gần đây tôi đã tìm thấy điều này và nghĩ rằng đáng để chia sẻ:

import os

path = "C:\\temp\myFolder\example\\"
newPath = path.replace(os.sep, '/')
print(newPath)  # -> C:/temp/myFolder/example/

Martineau

Huy hiệu vàng 115K2525 gold badges160 silver badges283 bronze badges

Hỏi ngày 5 tháng 8 năm 2014 lúc 19:39May 7, 2018 at 19:27

Gần đây tôi đã tìm thấy điều này và nghĩ rằng đáng để chia sẻ:Numabyte

Đã trả lời ngày 7 tháng 5 năm 2018 lúc 19:275 silver badges6 bronze badges

1

Numabytenumabyte

s.replace('\\', '/')

7405 Huy hiệu bạc6 Huy hiệu đồng

posixpath.join(*s.split('\\'))

Vấn đề cụ thể của bạn là thứ tự và thoát khỏi các đối số

C:\folderA\folderB to C:/folderA/folderB
1 của bạn, nên

os.path.join(*s.split('\\'))

Sau đó có:

Mà trên nền tảng *Nix tương đương với:

Nhưng đừng dựa vào điều đó trên Windows vì nó sẽ thích bộ phân cách dành riêng cho nền tảng. Cũng thế:Aug 5, 2014 at 19:47

Lưu ý rằng trên Windows, vì có một thư mục hiện tại cho mỗi ổ đĩa, os.path.join ("C:", "foo") đại diện cho một đường dẫn so với thư mục hiện tại trên ổ đĩa C: (c: foo), không phải c : \ foo.Jason S

Đã trả lời ngày 5 tháng 8 năm 2014 lúc 19:472 gold badges35 silver badges42 bronze badges

3

Jason Sjason s

path = '/'.join(path.split('\\'))

13.2k2 Huy hiệu vàng35 Huy hiệu bạc42 Huy hiệu đồngAug 5, 2014 at 19:41

ThửTheoretiCAL

Đã trả lời ngày 5 tháng 8 năm 2014 lúc 19:418 gold badges38 silver badges63 bronze badges

1

Lý thuyết

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
0

17.9k8 Huy hiệu vàng38 Huy hiệu bạc63 Huy hiệu Đồng

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
1

Tên đường dẫn được định dạng khác nhau trong Windows. Giải pháp rất đơn giản, giả sử bạn có một chuỗi đường dẫn như thế này:

Đơn giản là bạn phải thay đổi nó thành điều này: (thêm r trước đường dẫn)Jun 28, 2018 at 7:39

Các điều chỉnh trước khi chuỗi nói với Python rằng đây là một chuỗi thô. Trong các chuỗi thô, dấu gạch chéo ngược được giải thích theo nghĩa đen, không phải là một nhân vật trốn thoát.scapa

Đã trả lời ngày 28 tháng 6 năm 2018 lúc 7:391 silver badge6 bronze badges

1

Scapascapa

1191 Huy hiệu bạc6 Huy hiệu đồng is a module for "Object-oriented filesystem paths"

Xin lỗi vì đã trễ bữa tiệc, nhưng tôi tự hỏi không ai đề xuất thư viện pathlib.

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
2

Pathlib là một mô-đun cho "Đường dẫn hệ thống tập tin hướng đối tượng"

Để chuyển đổi từ Windows-Style (Backslash) -path thành những lần trượt chuyển tiếp (thường là cho các đường dẫn POSIX), bạn có thể làm như vậy theo kiểu rất dài dòng (và độc lập nền tảng) với Pathlib:May 14, 2021 at 14:55

Xin lưu ý rằng ví dụ sử dụng "r" theo nghĩa là chuỗi (để tránh có "\" làm đường thoát) trong các trường hợp khác, đường dẫn nên được trích dẫn đúng (với dấu gạch chéo ngược lại) "Stefan

Đã trả lời ngày 14 tháng 5 năm 2021 lúc 14:552 silver badges8 bronze badges

Stefanstefan

1112 Huy hiệu bạc8 Huy hiệu đồng

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
3

Để xác định biến của đường dẫn, bạn phải thêm

C:\folderA\folderB to C:/folderA/folderB
2 ban đầu, sau đó thêm câu lệnh thay thế
C:\folderA\folderB to C:/folderA/folderB
3 ở cuối.

Ví dụ:Mar 21, 2018 at 10:53

Giải pháp này không yêu cầu thư viện bổ sungMohammad ElNesr

Đã trả lời ngày 21 tháng 3 năm 2018 lúc 10:533 gold badges27 silver badges42 bronze badges

1

Mohammad Elnesrmohammad Elnesr

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
4

2.3473 huy hiệu vàng27 Huy hiệu bạc42 Huy hiệu đồngSep 28, 2017 at 6:29

1

Bạn nghĩ thế nào về :

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
5

print(slash_changer(os.getcwd()))

Đã trả lời ngày 28 tháng 9 năm 2017 lúc 6:29Nov 19, 2021 at 22:04

Điều này cũng có thể hoạt động:

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
6

result:

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
7

Đã trả lời ngày 19 tháng 11 năm 2021 lúc 22:04Mar 26 at 11:02

Làm thế nào để bạn gõ một chém về phía trước trong Python?

Đây là giải pháp hoàn hảo đặt chữ cái 'r' trước chuỗi mà bạn muốn chuyển đổi để tránh tất cả các ký tự đặc biệt thích '\ t' và '\ f' ... như ví dụ dưới đây:When specifying a path, a forward slash (/) can be used in place of a backslash.

Làm thế nào để bạn thay đổi chém lùi để chuyển tiếp về phía trước?

Đã trả lời ngày 26 tháng 3 lúc 11:02Press \/ to change every backslash to a forward slash, in the current line. Press \\ to change every forward slash to a backslash, in the current line.

Làm thế nào để bạn thay đổi dấu gạch chéo ngược thành một chuỗi trong Python?

Làm thế nào để bạn chém vào Python?use the replace() function to replace the backslashes in a string with another character. To replace all backslashes in a string, we can use the replace() function as shown in the following Python code.

Làm thế nào để bạn chém vào Python?

Tôi có ba cách tiếp cận:.

Trong mỗi kịch bản, đầu ra đã được

Tôi không chắc mình đang làm gì sai, bất kỳ đề xuất nào?

Martineau

Những vết chém nào được sử dụng trong Python?

Trong các chuỗi Python, dấu gạch chéo ngược "\" là một nhân vật đặc biệt, còn được gọi là nhân vật "Escape". Nó được sử dụng để thể hiện các ký tự khoảng trắng nhất định: "\ t" là một tab, "\ n" là một dòng mới và "\ r" là một sự trở lại vận chuyển."\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

Tôi có nên sử dụng Slash hay Backslash không?

Backslash chỉ được sử dụng để mã hóa máy tính.Sự chém về phía trước, thường được gọi là một dấu gạch chéo, là một dấu chấm câu được sử dụng trong tiếng Anh.Lần duy nhất thích hợp để sử dụng dấu phẩy sau khi chém là khi thể hiện sự phá vỡ giữa các dòng thơ, bài hát hoặc vở kịch.. The forward slash, often simply referred to as a slash, is a punctuation mark used in English. The only time it is appropriate to use a comma after a slash is when demonstrating breaks between lines of poetry, songs, or plays.

Slash về phía trước có ý nghĩa gì trong Python?

Sử dụng Slash / để chia mã thành mã đa dòng.Line Break có nghĩa là thay đổi dòng mã trong Python, nhưng bạn có thể sử dụng Slash / to Bluff Python.Bạn có thể dễ dàng chia mã của mình thành nhiều dòng bằng cách sử dụng chém chuyển tiếp ở giữa.Xem trên Trinket.io.break code into multiline code. Line break means code line change in Python, but you can use forward slash / to bluff python. You can easily break your code into multiple lines using forward slash in between. View on trinket.io.

Làm thế nào để bạn thực hiện một chém về phía trước trong Python?

Sử dụng phương thức str.Split () để phân chia một chuỗi trên các dấu gạch chéo phía trước, ví dụ:my_list = my_str.tách ra('/') . split() method to split a string on the forward slashes, e.g. my_list = my_str. split('/') .