![](/img/trans.png)
[英]How to list all files inside the folder of sharepoint using python
[英]How to prepend the prefix “_” to all files in a folder, in a list of files [Python]
每天我都在运行输出日志文件的脚本。 我有一个外部 Hive 表读取文件夹中没有前缀“_”的所有文件。 因此,每当我运行我的脚本时,我都需要在文件夹中所有以“.log”结尾且没有“_”前缀的文件前添加“_”。
我的文件夹结构是这样的。
-output
-_data-2020-04-10.log
-data-2020-04-11.log
我的代码目前是这样的
if __name__ == "__main__"
df = fetch_todays_data() #Returns dataframe
if not [f for f in os.listdir(dataPath) if not f.startswith('_') and f.endswith(".log")] == []:
fileset = [f for f in os.listdir(dataPath) if not f.startswith('_') and f.endswith(".log")]
for f in fileset:
#### prepend "_" to all files.
dataframe_to_json_log(output_path+/'data-{}'.format(datetime.date.today())) #Help function that transforms dataframe to json_blob in output folder
如何正确地将“_”添加到文件集中的所有fileset
?
编辑:
不知道append的意思,应该是前置。
与 TheMechanist 类似,但不影响已经具有“_”前缀的文件:
import glob, os
fileset = [f for f in glob.glob("*.log") if not f.startswith('_')]
for f in fileset:
os.rename(f, "_" + f)
您可以使用 glob:
from os.path import basename
import glob, os
path = 'mypath'
os.chdir(path)
basename(path)
for file in glob.glob("*.log"):
os.rename(basename, "_"+basename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.