Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用\\&引用;在文件路径os.path.join中_Python - Fatal编程技术网

Python &引用\\&引用;在文件路径os.path.join中

Python &引用\\&引用;在文件路径os.path.join中,python,Python,写一个pyhton程序把数据保存在.txt文件中,当创建文件路径时,它会出错,路径中有“\”为什么 该方案: #storage data in .txt file def data_save_txt(type,data,id,name): # get the date when storage data date_storage() #create the data storage directory txt_parent_directory = os.path.

写一个pyhton程序把数据保存在.txt文件中,当创建文件路径时,它会出错,路径中有“\”为什么

该方案:

#storage data in .txt file
def data_save_txt(type,data,id,name):
    # get the date when storage data
    date_storage()
    #create the data storage directory
    txt_parent_directory = os.path.join("dataset","txt",type,glovar.date)

    directory_create(txt_parent_directory)
    #write data in .txt
    if type == "group_members":
        txt_file_prefix = "gm"
    elif type == "group_feed":
        txt_file_prefix = "gf"
    elif type == "public_figure_posts":
        txt_file_prefix = "pfp"
    elif "user_" in type:
        txt_parent_directory = os.path.join("dataset", "txt", "user", type, glovar.date)
        txt_file_prefix = type
    txt_file_directory = os.path.join(txt_parent_directory,txt_file_prefix+"_"+time_storage()+"_"+id+"_"+name+".txt")
    txt_file_object = open(txt_file_directory,"w",encoding="utf-8")

    #to show the line number of stored data
    line_number = 1
    if isinstance(data,str):
        txt_file_object.write(data)
    elif isinstance(data,list):
        group_info_data = ''
        for i in range(len(data)):
            for (k, v) in data[i].items():
                group_info_data = group_info_data + str(line_number) + ")   " + k + ':' + str(v) + ','
            group_info_data += '\n'
            line_number += 1
        txt_file_object.write(group_info_data)
    txt_file_object.close()
当它运行时,会出现以下错误:

Traceback (most recent call last):
  File "C:/Python/PyCharmProject/FaceBookCrawl/FBCrawl.py", line 255, in <module>
    user_info_download.user_info_storage(user_id,user_info_type,u_access_token)
  File "C:\Python\PyCharmProject\FaceBookCrawl\user_info_download.py", line 79, in user_info_storage
    data_storage.data_save_txt(type,user_info,user_id,user_name)
  File "C:\Python\PyCharmProject\FaceBookCrawl\data_storage.py", line 29, in data_save_txt
    txt_file_object = open(txt_file_directory,"w",encoding="utf-8")
  FileNotFoundError: [Errno 2] No such file or directory: 'dataset\\txt\\user\\user_friends\\20170121\\user_friends_20170121-124531_110286969468305_Du Bin.txt'

Process finished with exit code 1

这句话是错误的,路径中有“\”,但我使用os.path.join,为什么,谁能帮我解决这个问题

我相信根据错误,您试图在一个不存在的目录中创建一个文件

也就是说,问题在于目录名,而不是文件名。

通过调用

txt_parent_directory = os.path.join("dataset", "txt", "user", type, glovar.date)
txt_file_directory = os.path.join(txt_parent_directory,txt_file_prefix+"_"+time_storage()+"_"+id+"_"+name+".txt")
您正在构建文件名字符串
'dataset\txt\user\user\u friends\20170121\user\u friends\u 20170121-124531\u 1102869468305\u Du Bin.txt'
双反斜杠只是Python显示某些特殊字符的方式

但它不会创建实际的文件夹。运行类似于

os.makedirs()

在尝试创建文件之前,请在目录上单击。双斜杠是python显示包含斜杠的字符串的正常方式。您的问题更可能是因为您的文件不存在/您当前的目录不是您认为的目录。如果您
print('dataset\\txt\\user\\user\u friends\\20170121\\user\u friends\u 20170121-124531\u 1102869468305\u Du Bin.txt'))
您将获得
数据集\txt\user\user\u friends\20170121\user\u friends\u 20170121-124531\u 1102869468305\u Du Bin.txt
-注意,没有双反斜杠。您看到的是字符串的表示形式,``转义字符本身就是转义的。
os.makedirs()