10 月 122020
 
#定义一个创建文件的函数
def create_file():
    path = 'C:/Users/harveymei/Desktop/' #最后一个斜线必须有
    fullname = path + filename + '.txt'
    file = open(fullname, 'w')
    file.close()

#使用for循环调用创建文件函数
for name in range(1,11): #使用range()函数提供1-10序列数值
    filename = str(name) #使用str()函数将整数转换为字符串赋值给变量
    create_file() #循环调用函数,创建10个文件
PS D:\Python> & C:/Users/harveymei/AppData/Local/Programs/Python/Python39/python.exe d:/Python/hello.py
PS D:\Python> ls C:\Users\harveymei\Desktop\


    目录: C:\Users\harveymei\Desktop


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2020/10/12     11:51              0 1.txt
-a----        2020/10/12     11:51              0 10.txt
-a----        2020/10/12     11:51              0 2.txt
-a----        2020/10/12     11:51              0 3.txt
-a----        2020/10/12     11:51              0 4.txt
-a----        2020/10/12     11:51              0 5.txt
-a----        2020/10/12     11:51              0 6.txt
-a----        2020/10/12     11:51              0 7.txt
-a----        2020/10/12     11:51              0 8.txt
-a----        2020/10/12     11:51              0 9.txt
-a----         2020/10/9      9:00           2136 Docker Desktop.lnk
-a----         2020/4/14      8:55           2371 GitHub Desktop.lnk
-a----         2020/8/28     15:20           2312 Kindle.lnk
-a----         2020/8/21     17:59           2255 MockingBot.lnk
-a----        2020/10/10      9:40           2217 Slack.lnk


PS D:\Python>

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)