有时,我必须删除服务器上不同文件夹中的多个文件。通过 FTP 逐一查看此内容是一项乏味的工作。我写了一个小脚本来完成这项工作。我可以在 Excel 中导入文件列表,然后将它们复制并粘贴到脚本中。然而,我的方法并不优雅:
$file1 = "some-file.php";
$file12 = "some-file2.php";
...
if (!empty($file1)) {
if ( @unlink ( $_SERVER[DOCUMENT_ROOT]."/".$file1 ) )
{
echo 'The file <strong><span style="color:green;">' . $file1 . '</span></strong> was deleted!<br />';
}
else
{
echo 'Couldn't delete the file <strong><span style="color:red;">' . $file1 . '</span></strong>!<br />';
}}
if (!empty($file2)) { ...
Run Code Online (Sandbox Code Playgroud)
我宁愿用 foreach 和数组来做到这一点,但不知道如何。任何帮助,将不胜感激!
例如,我们的 file.getPath() 返回“Data\Cache\Character\images\1.png” 现在我想做的就是将 String 或其他路径或其他路径设置为:“Character\images\1.png”。 png”因此从一开始就删除了前两个文件夹。谢谢。
我对正则表达式很不好。我正在尝试根据文件名在文件夹中查找文件。大多数文件名都是格式GSE1234_series_matrix.txt,因此我一直使用os.path.join("files", GSE_num + "_series_matrix.txt"). 但是,有一些文件的名称类似于GSE1234-GPL22_series_matrix.txt. 我不确定如何处理以 GSE 编号开头并以 _series_matrix.txt 结尾的所有文件(可能在一个语句中)。我真的很感激任何帮助。
编辑 - 我在一个文件夹中有这些系列矩阵文本文件,我在其中提到了使用路径连接的路径。我还输入了一个文本文件,其中包含所有 GSE 编号。这样,它仅针对选定的 GSE 编号运行脚本。因此,并非文件夹中的所有内容都在 GSE 编号列表中,并且该列表仅包含 GSE 编号,而不包含 GPL。例如,文件 GSE1234-GPL22_series_matrix.txt 在列表中将是 GSE1234。
我有一个很大的 json 数据,我想从根获取所有路径,直到获取路径的值,然后将结果存储到字符串中,如下所示,这里是我的 json
{
"root":{
"first":{
"first1":{
"value":"1"
},
"first2":{
"value":"2"
},
"first3":{
"value":"3"
}
},
"second":{
"second1":{
"value":"1"
},
"second2":{
"value":"2"
},
"second3":{
"value":"3"
}
},
"third":{
"third1":{
"value":"1"
},
"third2":{
"value":"2"
},
"third3":{
"value":"3"
}
},
"four":{
"value":"4"
},
"five":{
"five1":{
"five11":{
"value":"five11"
},
"five12":{
"value":"five12"
}
},
"five2":{
"five21":{
"five211":{
"value":"five211"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我想在 C# 中动态地创建每个路径,并在屏幕中显示,请告诉我一种方法
root.first.first1.value
root.first.first2.value
root.first.first3.value
root.second.second1.value
......
root.four.value
root.five.five1.five11.value
root.five.five1.five12.value
....
root.five2.five21.five211.value
Run Code Online (Sandbox Code Playgroud) 假设我有一个代表目录的类(当然是简化的示例):
import os
class Dir:
def __init__(self, path):
self.path = os.path.normcase(path)
Run Code Online (Sandbox Code Playgroud)
为了使事情更容易在内部实现,我在将参数保存到属性之前调用os.path.normcase该path参数。这很好用,但它会小写路径:
>>> import os
>>> os.path.normcase(r'C:\Python34\Lib')
'c:\\python34\\lib'
>>>
Run Code Online (Sandbox Code Playgroud)
我想要一种方法将路径转回其正确的大写形式C:\Python34\Lib。我计划在方法内部执行此操作__repr__,以便获得不错的输出,例如:
>>> my_dir
Dir(r'C:\Python34\Lib')
>>>
Run Code Online (Sandbox Code Playgroud)
当我在交互式翻译中时。标准库中有类似的东西吗?
注意:我不是指用户作为参数提供的字符串path。如果用户这样做:
my_dir = Dir('c:\PYTHON34\lib')
Run Code Online (Sandbox Code Playgroud)
我仍然想Dir('C:\Python34\Lib')在解释器中打印,因为这是正确的大写。基本上,我希望输出的路径与文件资源管理器中的路径相同。
我想要做的一个简化版本是像这样转动一棵树:

进入这样的数组:[“abc”,“abd”,“ae”]
基本上我想从根节点遍历树到每个子节点。
我尝试通过在递归块中放置一个 for-in 循环来实现此目的,但问题是每次递归块时 for 循环都会重新开始。当我尝试异步运行该块时,我不断收到 EXC_BAD_ACCESS
有什么建议么?
我试图列出新安装的 Xubuntu 12.04 中已安装的软件包:
for d in `echo "${PYTHONPATH}" | tr ':' '\n'`; do ls "${d}"; done
Run Code Online (Sandbox Code Playgroud)
但在我一无所获之后,我发现我错过了 PYTHONPATH。然后我在 Xubuntu 14.04 上尝试了一下,得到了同样的结果。我试图找出为什么我缺少这个变量,但到处都提到只改变它。谢谢
这似乎是文件路径的常见错误,但我的问题更奇怪,因为代码昨天工作正常,但今天不行(而且我没有更改任何代码)。我的文件夹目录很简单:
-node_modules
-public
-css
-js
control_panel.html
index.html
app.js
packages.json
Run Code Online (Sandbox Code Playgroud)
我在 app.js 中使用 Express 中间件来帮助渲染文件。
var express = require("express");
var app = express();
app.use(express.static("public"));
app.get('/', function get(req, res) {
res.sendFile('/index.html');
});
app.get('/control_panel', function get(req, res) {
res.sendFile('/control_panel.html');
});
Run Code Online (Sandbox Code Playgroud)
当我尝试在浏览器中打开index.html时,没有问题,一切都按预期进行。然而,当我尝试在浏览器中打开 control_panel.html 时,我得到Error: ENOENT: no such file or directory, stat '/control_panel.html' at Error (native)
是什么导致了这个问题?
我想按年份对这个数组进行排序:
Array
(
[0] => data/pictures/alice/1980
[1] => data/pictures/alice/1985
[2] => data/pictures/bob/1981
[3] => data/pictures/bob/1985
[4] => data/pictures/bob/1987
[5] => data/pictures/bob/1989
)
Run Code Online (Sandbox Code Playgroud)
预期结果:
Array
(
[0] => data/pictures/alice/1980
[1] => data/pictures/bob/1981
[2] => data/pictures/alice/1985
[3] => data/pictures/bob/1985
[4] => data/pictures/bob/1987
[5] => data/pictures/bob/1989
)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过不同的排序功能但没有成功。
例子:
asort($paths, SORT_STRING | SORT_FLAG_CASE);
sort($path, SORT_NUMERIC);
Run Code Online (Sandbox Code Playgroud) path ×10
python ×3
file ×2
filepath ×2
php ×2
arrays ×1
basename ×1
bash ×1
c# ×1
command-line ×1
directory ×1
express ×1
java ×1
json ×1
node.js ×1
objective-c ×1
python-2.7 ×1
python-3.x ×1
recursion ×1
regex ×1
response ×1
sorting ×1
string ×1
tree ×1
unlink ×1
webstorm ×1
windows ×1
xubuntu ×1