簡體   English   中英

構造路徑上的“ FileNotFoundError:[Errno 2] Windows中沒有這樣的文件或目錄”

[英]“FileNotFoundError:[Errno 2] No such file or directory in windows” on constructed path

我的文件路徑是

C:/Users/Ratul/Downloads/Machine_Learning_Data/reddit_data/reddit_data/

該目錄中有許多文件夾。 我需要瀏覽這些目錄並打開以“ RC_”開頭的文件

從圖片可以看出

這是我的代碼:

import sqlite3
import json
import os
from datetime import datetime

timeframe = '2015-05'
sql_transaction = []

connection = sqlite3.connect('{}.db'.format(timeframe))
c = connection.cursor()

def create_table():
    c.execute("CREATE TABLE IF NOT EXISTS parent_reply(parent_id TEXT PRIMARY KEY, comment_id TEXT UNIQUE, parent TEXT, comment TEXT, subreddit TEXT, unix INT, score INT)")

def format_data(data):
    data = data.replace('\n',' newlinechar ').replace('\r',' newlinechar ').replace('"',"'")
    return data

def find_parent(pid):
    try:
        sql = "SELECT comment FROM parent_reply WHERE comment_id = '{}' LIMIT 1".format(pid)
        c.execute(sql)
        result = c.fetchone()
        if result != None:
            return result[0]
        else: return False
    except Exception as e:
        #print(str(e))
        return False


if __name__ == '__main__':
    create_table()
    row_counter = 0
    paired_rows = 0

    with open('C:/Users/Ratul/Downloads/Machine_Learning_Data/reddit_data/reddit_data/{}/RC_{}'.format(timeframe.split('-')[0],timeframe), buffering=1000) as f:
        for row in f:
            row_counter += 1
            row = json.loads(row)
            parent_id = row['parent_id']
            body = format_data(row['body'])
            created_utc = row['created_utc']
            score = row['score']
            comment_id = row['name']
            subreddit = row['subreddit']
            parent_data = find_parent(parent_id)
            # maybe check for a child, if child, is our new score superior? If so, replace. If not...

            if score >= 2:
                existing_comment_score = find_existing_score(parent_id)

但似乎這條道路上有一些錯誤。 我得到一個錯誤

追溯(最近一次通話最近):文件“ C:/Users/Ratul/AppData/Local/Programs/Python/Python37/test02.py”,第36行,其中open('C:/ Users / Ratul / Downloads / Machine_Learning_Data /reddit_data/reddit_data/{}/RC_{}'.format(timeframe.split('-')[0],timeframe),buffering = 1000)as f:FileNotFoundError:[Errno 2] No such file or directory:' C:/用戶/ Ratul /下載/ Machine_Learning_Data / reddit_data / reddit_data / 2015 / RC_2015-05'

我不確定在那做錯什么。 請幫忙。

使用如何調試小程序(#1)

print('C:/Users/Ratul/Downloads/Machine_Learning_Data/reddit_data/reddit_data/{}/RC_{}'.format(
       timeframe.split('-')[0],timeframe))

而不是open 檢查是否全部存在-因為對於您的某些值而言, 它不存在 因此,錯誤。

如果大多數文件都存在,則處理錯誤本身要容易得多:

myname = 'C:/Users/Ratul/Downloads/Machine_Learning_Data/reddit_data/reddit_data/{}/RC_{}'.format(timeframe.split('-')[0],timeframe)

try:
    with open(myname, buffering=1000) as f:
        for row in f:
            row_counter += 1
            row = json.loads(row)
            parent_id = row['parent_id']
            body = format_data(row['body'])
            created_utc = row['created_utc']
            score = row['score']
            comment_id = row['name']
            subreddit = row['subreddit']
            parent_data = find_parent(parent_id)
            # maybe check for a child, if child, is our new score superior? If so, replace. If not...

            if score >= 2:
                existing_comment_score = find_existing_score(parent_id)
except FileNotFoundError as fnfError:
    print(myname)
    print(fnfError)

open()命令並不關心您使用\\/ -如果使用\\則應轉義它或使用原始字符串(aka: r'C:\\some\\dir\\file.txt' )-您的語法可以正常使用'c:/somedir/file.txt' ()將在Windows下使用適當的目錄分隔符,即使您將其指定為'c:/somedir/file.txt'

閱讀: 關於錯誤處理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM