Python 如何检测用户何时按enter as input()

Python 如何检测用户何时按enter as input(),python,python-3.x,terminal,Python,Python 3.x,Terminal,我正在编写的脚本的一部分涉及将文件传递到终端应用程序的其他方面。这是我的密码: def get_file(): custom_data_folder = input("Name of custom data directory - " + "Hit ENTER to use default: ") file_name = input("File name: ") default_path = os.path.abspath(os.path.

我正在编写的脚本的一部分涉及将文件传递到终端应用程序的其他方面。这是我的密码:

def get_file():

custom_data_folder = input("Name of custom data directory - " +
                           "Hit ENTER to use default: ")
file_name = input("File name: ")
default_path = os.path.abspath(os.path.join('data', file_name))
custom_path = os.path.abspath(os.path.join(custom_data_folder, file_name))

if custom_data_folder == '\n':
    return(default_path if os.path.exists(default_path) is True 
                     else "The file or path does not exist.")
else:
    return(custom_path if os.path.exists(custom_path) is True 
                       else "The file or path does not exist.")
我的脚本假定一个文件位于一个名为“data”的目录中,但我想提供一个选项,可以输入一个自定义目录,其中可以包含一个文件

当我尝试使用“/n”换行符来检测用户是否按enter键时,就会出现问题。它似乎只是返回工作目录以及其中的文件,而不是我想要的“数据”目录及其文件

如何检测用户是否按enter键


我在linux/ubuntu中工作。单独针对该平台的解决方案很好,但如果可能的话,我更喜欢多平台解决方案

如果用户在没有输入任何内容的情况下按enter键,则返回空字符串。您可以将if语句更改为以下内容

if custom_data_folder == '':

如果用户未在输入中键入任何内容就按enter键,则返回空字符串。您可以将if语句更改为以下内容

if custom_data_folder == '':