在python中访问外部硬盘驱动器

时间:2019-02-18 12:08:13

标签: python macos

我最终试图读取连接到Mac的外部硬盘驱动器(名为“ toshiba”)中的文件夹(名为“ test”)的文件详细信息。文件名和大小,然后将其输出到文本文件。但是,我想知道为什么下面的代码不能正确输出?

import os

testdir = "/volumes/toshiba"
for folder in testdir:
    print folder

我希望它能输出硬盘驱动器上的所有文件夹,包括“ test”,但是它会输出

/
v
o
l
u
m
e
s
/
t
o
s
h
i
b
a

1 个答案:

答案 0 :(得分:1)

这里:

const Alexa = require('ask-sdk');
const dbHelper = require('./helpers/dbHelper');
const GENERAL_REPROMPT = "What would you like to do?";

const dynamoDBTableName = "movies";
const LaunchRequestHandler = 
{ 
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        return request.type === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speechText = 'Welcome to the X ' + '&' + 'Y Customer Service, my name is Natalia, how may I help you today?';
        const repromptText = 'What would you like to do? You can say HELP to get available options';

        return handlerInput.responseBuilder
        .speak(speechText)
        .reprompt(repromptText)
        .getResponse();
    }
};
const InProgressAddMovieIntentHandler = {
canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest' &&
    request.intent.name === 'AddMovieIntent' &&
    request.dialogState !== 'COMPLETED';
},
handle(handlerInput) {
    const currentIntent = handlerInput.requestEnvelope.request.intent;
    return handlerInput.responseBuilder
    .addDelegateDirective(currentIntent)
    .getResponse();
}
};
const AddMovieIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
    && handlerInput.requestEnvelope.request.intent.name === 'AddMovieIntent';
},
async handle(handlerInput) {
    const {responseBuilder } = handlerInput;
    const userID = handlerInput.requestEnvelope.context.System.user.userId; 
    const slots = handlerInput.requestEnvelope.request.intent.slots;
    const movieName = slots.MovieName.value;
    return dbHelper.addMovie(movieName, userID)
    .then((data) => {
        const speechText = `You have added movie ${movieName}. You can say add to add another one or remove to remove movie`;
        return responseBuilder
        .speak(speechText)
        .reprompt(GENERAL_REPROMPT)
        .getResponse();
    })
    .catch((err) => {
        console.log("Error occured while saving movie", err);
        const speechText = "we cannot save your movie right now. Try again!"
        return responseBuilder
        .speak(speechText)
        .getResponse();
    })
},
};
const GetMoviesIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
    && handlerInput.requestEnvelope.request.intent.name === 'GetMoviesIntent';
},
async handle(handlerInput) {
    const {responseBuilder } = handlerInput;
    const userID = handlerInput.requestEnvelope.context.System.user.userId; 
    return dbHelper.getMovies(userID)
    .then((data) => {
        var speechText = "Your movies are "
        if (data.length == 0) {
        speechText = "You do not have any favourite movie yet, add movie by saving add moviename "
        } else {
        speechText += data.map(e => e.movieTitle).join(", ")
        }
        return responseBuilder
        .speak(speechText)
        .reprompt(GENERAL_REPROMPT)
        .getResponse();
    })
    .catch((err) => {
        const speechText = "we cannot get your movie right now. Try again!"
        return responseBuilder
        .speak(speechText)
        .getResponse();
    })
}
};
const InProgressRemoveMovieIntentHandler = {
canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest' &&
    request.intent.name === 'RemoveMovieIntent' &&
    request.dialogState !== 'COMPLETED';
},
handle(handlerInput) {
    const currentIntent = handlerInput.requestEnvelope.request.intent;
    return handlerInput.responseBuilder
    .addDelegateDirective(currentIntent)
    .getResponse();
}
};
const RemoveMovieIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
    && handlerInput.requestEnvelope.request.intent.name === 'RemoveMovieIntent';
}, 
handle(handlerInput) {
    const {responseBuilder } = handlerInput;
    const userID = handlerInput.requestEnvelope.context.System.user.userId; 
    const slots = handlerInput.requestEnvelope.request.intent.slots;
    const movieName = slots.MovieName.value;
    return dbHelper.removeMovie(movieName, userID)
    .then((data) => {
        const speechText = `You have removed movie with name ${movieName}, you can add another one by saying add`
        return responseBuilder
        .speak(speechText)
        .reprompt(GENERAL_REPROMPT)
        .getResponse();
    })
    .catch((err) => {
        const speechText = `You do not have movie with name ${movieName}, you can add it by saying add`
        return responseBuilder
        .speak(speechText)
        .reprompt(GENERAL_REPROMPT)
        .getResponse();
    })
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
    && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
    const speechText = 'You can access information about products, place an order or raise and issue';
    const repromptText = 'What would you like to do? You can say HELP to hear the options again';

    return handlerInput.responseBuilder
    .speak(speechText)
    .reprompt(repromptText)
    .getResponse();
},
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
    && (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
        || handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
    const speechText = 'Goodbye! See you later!';

    return handlerInput.responseBuilder
    .speak(speechText)
    .getResponse();
},
};
const FallbackHandler = {
canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest' && request.intent.name === 'AMAZON.FallbackIntent';
},
handle(handlerInput) {
    const output = "I am sorry, I can't help with that";
    return handlerInput.responseBuilder
    .speak(output)
    .reprompt(output)
    .getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
    console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);

    return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
    return true;
},
handle(handlerInput, error) {
    console.log(`Error handled: ${error.message}`);

    return handlerInput.responseBuilder
    .speak('Sorry, I can\'t understand the command. Please say again.')
    .reprompt('Sorry, I can\'t understand the command. Please say again.')
    .getResponse();
},
};
const skillBuilder = Alexa.SkillBuilders.standard();
exports.handler = skillBuilder
.addRequestHandlers(
    LaunchRequestHandler,
    InProgressAddMovieIntentHandler,
    AddMovieIntentHandler,
    GetMoviesIntentHandler,
    InProgressRemoveMovieIntentHandler,
    RemoveMovieIntentHandler,
    HelpIntentHandler,
    CancelAndStopIntentHandler,
    SessionEndedRequestHandler,
    FallbackHandler
)
.addErrorHandlers(ErrorHandler)
.withTableName(dynamoDBTableName)
.withAutoCreateTable(true)
.lambda();

您定义一个字符串(一个字符序列),然后对其进行迭代。该字符序列。当然,当您打印一系列字符时,除了一系列字符以外,还应该打印什么?!

相反,您想将该字符串传递到诸如testdir = "/volumes/toshiba" for folder in testdir: 之类的命令中,该命令将返回代表指定文件系统条目实际内容的“类似路径”对象的列表。