创建一个文本文件,然后通过电子邮件发送该文件而不在本

时间:2018-03-08 19:51:44

标签: python email text

我想在Python中创建一个文本文件,写一些东西,然后通过电子邮件发送(将test.txt作为电子邮件附件)。

但是,我无法在本地保存此文件。有谁知道怎么做呢?

只要我打开要写入的文本文件,它就会在本地保存在我的计算机上。

f = open("test.txt","w+")

我正在使用smtplibMIMEMultipart发送邮件。

1 个答案:

答案 0 :(得分:1)

StringIO是要走的路......

public class BlinkActivity extends Activity {
private static final String TAG = BlinkActivity.class.getSimpleName();
private static final int INTERVAL_BETWEEN_BLINKS_MS = 1000;

private Handler mHandler = new Handler();
private Gpio mLedGpio;
private boolean mLedState = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "Starting BlinkActivity");

    try {
        String pinName = BoardDefaults.getGPIOForLED();
        mLedGpio = PeripheralManager.getInstance().openGpio(pinName);
        mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
        Log.i(TAG, "Start blinking LED GPIO pin");
        // Post a Runnable that continuously switch the state of the GPIO, blinking the
        // corresponding LED
        mHandler.post(mBlinkRunnable);
    } catch (IOException e) {
        Log.e(TAG, "Error on PeripheralIO API", e);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // Remove pending blink Runnable from the handler.
    mHandler.removeCallbacks(mBlinkRunnable);
    // Close the Gpio pin.
    Log.i(TAG, "Closing LED GPIO pin");
    try {
        mLedGpio.close();
    } catch (IOException e) {
        Log.e(TAG, "Error on PeripheralIO API", e);
    } finally {
        mLedGpio = null;
    }
}

private Runnable mBlinkRunnable = new Runnable() {
    @Override
    public void run() {
        // Exit Runnable if the GPIO is already closed
        if (mLedGpio == null) {
            return;
        }
        try {
            // Toggle the GPIO state
            mLedState = !mLedState;
            mLedGpio.setValue(mLedState);
            Log.d(TAG, "State set to " + mLedState);

            // Reschedule the same runnable in {#INTERVAL_BETWEEN_BLINKS_MS} milliseconds
            mHandler.postDelayed(mBlinkRunnable, INTERVAL_BETWEEN_BLINKS_MS);
        } catch (IOException e) {
            Log.e(TAG, "Error on PeripheralIO API", e);
        }
    }
};
}