Day13 【小程序】文件修改

文件修改方法:1.加载到内存中修改;2.打开一个文件,修改后写到新文件中

f=open('yesterday','r',encoding='utf-8')

f_new=open('yesterday.bak','w',encoding='utf-8')
find_str=input('Find words:')
replace_str=input('Replace words:')
for line in f: if find_str in line: line=line.replace(find_str,replace_str) f_new.write(line) f.close() f_new.close()

 通过脚本传参数的方法

import sys

f=open('yesterday','r',encoding='utf-8')
f_new=open('yesterday.bak','w',encoding='utf-8')

find_str=sys.argv[1]
replace_str=sys.argv[2]
for line in f:
    if find_str in line:
        line=line.replace(find_str,replace_str)
    f_new.write(line)

 

转载于:https://www.cnblogs.com/q1ang/p/8894154.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是两种实现小程序夜间模式的代码示例: 1. 使用全局样式设置夜间模式 app.wxss 文件中定义夜间模式下的全局样式: ``` /* 夜间模式 */ .night-mode { background-color: #000; color: #fff; } /* 日间模式 */ .day-mode { background-color: #fff; color: #000; } ``` app.js 文件中根据夜间模式状态动态加载样式文件: ``` App({ onLaunch: function () { const nightMode = wx.getStorageSync('nightMode') || false; // 获取本地缓存中的夜间模式状态 if (nightMode) { wx.setStorageSync('nightMode', true); // 将夜间模式的状态存储在本地缓存中 wx.setStorageSync('mode', 'night-mode'); // 将夜间模式的样式文件名存储在本地缓存中 } else { wx.setStorageSync('nightMode', false); wx.setStorageSync('mode', 'day-mode'); } }, globalData: { mode: wx.getStorageSync('mode') || 'day-mode' // 根据本地缓存中的夜间模式状态动态加载样式文件 } }) ``` 切换夜间模式的代码实现: ``` toggleNightMode() { const nightMode = !wx.getStorageSync('nightMode'); // 取反操作获取当前夜间模式状态 wx.setStorageSync('nightMode', nightMode); // 将夜间模式的状态存储在本地缓存中 if (nightMode) { wx.setStorageSync('mode', 'night-mode'); // 将夜间模式的样式文件名存储在本地缓存中 } else { wx.setStorageSync('mode', 'day-mode'); } this.globalData.mode = wx.getStorageSync('mode'); wx.setNavigationBarColor({ frontColor: '#ffffff', backgroundColor: '#000000' }); wx.reLaunch({ url: '/pages/index/index' }); // 重新加载首页 } ``` 2. 在小程序页面中设置夜间模式 在页面的 .wxml 文件中添加切换按钮: ``` <view class="toggle-btn" bindtap="toggleNightMode">{{ nightMode ? '日间模式' : '夜间模式' }}</view> ``` 在页面的 .js 文件中实现切换夜间模式的函数: ``` toggleNightMode() { const nightMode = !this.data.nightMode; wx.setStorageSync('nightMode', nightMode); // 将夜间模式的状态存储在本地缓存中 this.setData({ nightMode: nightMode, bgColor: nightMode ? '#000000' : '#ffffff' }); wx.setNavigationBarColor({ frontColor: nightMode ? '#ffffff' : '#000000', backgroundColor: nightMode ? '#000000' : '#ffffff' }); } ``` 需要注意的是,以上代码示例仅供参考,实际开发中还需要根据具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值