简体   繁体   English

如何将结果转化为变量?

[英]How do I turn my outcome into a variable?

import glob, os
os.chdir("DIRECTORY LIST OF .ACCDB FILES")
for file in glob.glob("*.accdb"):
    print(file)

Example of what the output is:输出示例:

  1. Attachment1.accdb附件1.accdb
  2. Attachment2.accdb附件2.accdb
  3. Attachment3.accdb附件3.accdb
  4. Attachment4.accdb附件4.accdb
  5. Attachment5.accdb附件5.accdb
  6. Attachment6.accdb附件6.accdb

Here is the code I am starting off with.这是我开始使用的代码。 It spits out the list of all the .ACCDB files into my folder directory.它将所有 .ACCDB 文件的列表吐出到我的文件夹目录中。 But now I want to take all of that data and turn it into a variable.但是现在我想获取所有这些数据并将其转换为变量。 Any help would be most appreciated.非常感激任何的帮助。 I am very new to programming.我对编程很陌生。

You can assign a variable directly to the return value of glob.glob :您可以将变量直接分配给glob.glob的返回值:

accdb_files = glob.glob("*.accdb")

Additionally, rather than use os.chdir to change paths, you may want to just use the path into the glob.glob() argument:此外,您可能只想使用glob.glob()参数中的路径,而不是使用os.chdir来更改路径:

import glob

accdb_files = glob.glob("/example/path/to/attachments/*.accdb")

This way, it's easier to read, and you don't need keep the state of the current working directory for other file operations.这样,更容易阅读,并且不需要为其他文件操作保留当前工作目录的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM