FileNotFoundError, os.getcwd () возвращает имя файла не в каталог


0

Я бы использовал input()вместо этого def readFile(list).

Вот вам необязательный код, если вы правильно поняли:

#!/usr/bin/env python

'''
1. Read a file.
1a. If file can be read, generate a list from that file.
1b. Return a random selection from that list to the user.
'''

'''
2. If file cannot be read or error: alert user with message
2a. Use default list.
'''

import os
import pathlib
import random

#Print the current working directory to check if path is correct.

cwd = os.getcwd()
print("This is the directory the desired file is located: " + cwd)

#Request input from user for name of file. 

file_desired = input() + '.txt'

#Use pathlib to set path of the file. 

path = pathlib.Path('write_full_path_to_directory_here' + file_desired)
print(path)

#If the path of the file exists, use list from file.

if path.is_file():
    with open (file_desired, 'r') as file_wanted:
        print("File is available.")
        dictionary = file_wanted.readlines()
        Activitylist = [word.strip() for word in dictionary]
        print(random.choice(Activitylist))

#If file path does not exist, then use default list.

else:
    print('Sorry, all activities list, file not found')
    print('Using default all activities list...
')

    chores = ['Washing Up', 'Laundry']
    fun = ['Watch TV', 'Play a game']

    allActivities = chores + fun

    print(random.choice(allActivities))

Надеюсь, это поможет!

Omneya

питон-3.x, файл-не-найден,

python-3.x,file-not-found,

0

Ответов: 1


0

Я бы использовал input()вместо этого def readFile(list).

Вот вам необязательный код, если вы правильно поняли:

#!/usr/bin/env python

'''
1. Read a file.
1a. If file can be read, generate a list from that file.
1b. Return a random selection from that list to the user.
'''

'''
2. If file cannot be read or error: alert user with message
2a. Use default list.
'''

import os
import pathlib
import random

#Print the current working directory to check if path is correct.

cwd = os.getcwd()
print("This is the directory the desired file is located: " + cwd)

#Request input from user for name of file. 

file_desired = input() + '.txt'

#Use pathlib to set path of the file. 

path = pathlib.Path('write_full_path_to_directory_here' + file_desired)
print(path)

#If the path of the file exists, use list from file.

if path.is_file():
    with open (file_desired, 'r') as file_wanted:
        print("File is available.")
        dictionary = file_wanted.readlines()
        Activitylist = [word.strip() for word in dictionary]
        print(random.choice(Activitylist))

#If file path does not exist, then use default list.

else:
    print('Sorry, all activities list, file not found')
    print('Using default all activities list...
')

    chores = ['Washing Up', 'Laundry']
    fun = ['Watch TV', 'Play a game']

    allActivities = chores + fun

    print(random.choice(allActivities))

Надеюсь, это поможет!

Omneya

питон-3.x, файл-не-найден,
Похожие вопросы
Яндекс.Метрика