首页 \ 问答 \ 在Python中重命名一个目录中的多个文件[duplicate](Rename multiple files in a directory in Python [duplicate])

在Python中重命名一个目录中的多个文件[duplicate](Rename multiple files in a directory in Python [duplicate])

这个问题已经在这里有一个答案:

  • 如何重命名一个文件使用Python 6答案
  • 在Python 1中 重命名多个文件

我试图使用Python重命名一个目录中的一些文件。

说我有一个名为CHEESE_CHEESE_TYPE.***的文件CHEESE_CHEESE_TYPE.***并且要删除CHEESE_所以我的结果文件CHEESE_TYPECHEESE_TYPE

我试图使用os.path.split但它不能正常工作。 我也考虑过使用字符串操作,但是还没有成功。


This question already has an answer here:

I'm trying to rename some files in a directory using Python.

Say I have a file called CHEESE_CHEESE_TYPE.*** and want to remove CHEESE_ so my resulting filename would be CHEESE_TYPE

I'm trying to use the os.path.split but it's not working properly. I have also considered using string manipulations, but have not been successful with that either.


原文:https://stackoverflow.com/questions/2759067
更新时间:2023-05-23 19:05

最满意答案

要详细说明driushkin的答案,您应该使用远程过程调用(RPC)和事件队列。 这类似于您发布的图像,其中每个数据包代表一个“命令”或带有一些参数的RPC(即移动方向)。 您还需要一个事件队列来确保按顺序和按时执行RPC。 这将需要时间戳或帧计数来执行每个命令(在未来的某个时间点,以简单的方案)和同步手表(第二次世界大战风格)。

您可能会注意到此方案中的一个关键弱点:由于网络延迟,恶意用户等原因,RPC消息可能会延迟(应在应用之后到达)。在一个简单的方案中,会丢弃延迟的RPC。 这很好,因为所有客户端(甚至是发起人!)等待服务器在执行之前发送RPC(如果原始客户端没有等待服务器消息,他的游戏状态将与服务器不同步,并且您的游戏将被打破)。

考虑滞后对这种方案的影响。 假设客户端A到服务器的延迟是100毫秒,返回旅程也是100毫秒。 这意味着客户端输入如下:

  • 客户端A按键,并将RPC发送到服务器, 但不在本地添加 (0ms)
  • 服务器接收并重播RPC(100ms)
  • 客户端A收到他自己的事件,现在最终将其添加到他的事件队列中进行处理(200ms)

正如您所看到的,客户在按下按键后1/5秒响应自己的事件。 这是相当不错的100毫秒滞后。 跨越延迟每个方向很容易超过200毫秒,拨号连接(很少见,但今天仍然存在)可能会有超过500毫秒的延迟峰值。 如果你在局域网或类似的东西上玩,这一切都不重要,但在互联网上,这种反应迟钝可能是无法忍受的。

这就是客户端预测(CSP)的概念所在.CSP被认为是大而可怕的,但是正确实施并且经过深思熟虑它实际上非常简单。 CSP的一个有趣特性是客户可以立即处理他们的输入 (客户端预测会发生什么)。 当然,客户可以(并且经常会)出错。 这意味着客户端需要一种从服务器应用更正的方法。 这意味着您需要一种方法让服务器验证,拒绝或修改来自客户端的RPC请求,以及一种序列化游戏状态的方法(因此可以将其恢复为从中重新模仿的基点)。

这样做有很多很好的资源。 我特别喜欢http://www.gabrielgambetta.com/?p=22 ,但你真的应该寻找一款好的多人游戏编程书。


即使在阅读了有关Flex和AS3的评论之后,我也必须建议socket.io。 易用(以及与节点的简单集成)使其成为我曾经使用过的基于HTTP的网络游戏的最佳(最佳?)选项之一。 我会进行必要的调整以便能够使用它。 我相信AIR / AS3至少有一个WebSockets库,即使socket.io本身不可用。


To elaborate on driushkin's answer, you should use remote procedure calls (RPC) and an event queue. This works like in the image you've posted, where each packet represents a 'command' or RPC with some arguments (i.e. movement direction). You'll also need an event queue to make sure RPCs are executed in order and on time. This will require a timestamp or framecount for each command to be executed on (at some point in the future, in a simple scheme), and synchronized watches (World War II style).

You might notice one critical weakness in this scheme: RPC messages can be late (arrive after the time they should be applied) due to network latency, malicious users, etc. In a simple scheme, late RPCs are dropped. This is fine since all clients (even the originator!) wait for the server to send an RPC before acting (if the originating client didn't wait for the server message, his game state would be out of sync with the server, and your game would be broken).

Consider the impact of lag on such a scheme. Let's say the lag for Client A to the server was 100ms, and the return trip was also 100ms. This means that client input goes like:

  • Client A presses key, and sends RPC to server, but doesn't add it locally (0ms)
  • Server receives and rebroadcasts RPC (100ms)
  • Client A receives his own event, and now finally adds it to his event queue for processing (200ms)

As you can see, the client reacts to his own event 1/5 of a second after he presses the key. This is with fairly nice 100ms lag. Transoceanic lag can easily be over 200ms each way, and dialup connections (rare, but still existent today) can have lag spikes > 500ms. None of this matters if you're playing on a LAN or something similar, but on the internet this unresponsiveness could be unbearable.

This is where the notion of client side prediction (CSP) comes in. CSP is made out to be big and scary, but implemented correctly and thoughtfully it's actually very simple. The interesting feature of CSP is that clients can process their input immediately (the client predicts what will happen). Of course, the client can (and often will) be wrong. This means that the client will need a way of applying corrections from the Server. Which means you'll need a way for the server to validate, reject, or amend RPC requests from clients, as well as a way to serialize the gamestate (so it can be restored as a base point to resimulate from).

There are lots of good resources about doing this. I like http://www.gabrielgambetta.com/?p=22 in particular, but you should really look for a good multiplayer game programming book.


I also have to suggest socket.io, even after reading your comments regarding Flex and AS3. The ease of use (and simple integration with node) make it one of the best (the best?) option(s) for network gaming over HTTP that I've ever used. I'd make whatever adjustments necessary to be able to use it. I believe that AIR/AS3 has at least one WebSockets library, even if socket.io itself isn't available.

相关问答

更多
  • 这些原则是否适用于TCP,就像它们对UDP的做法一样,在实现方面会有什么不同吗? 我可以看到,实体插值不需要防止数据包丢失,但是这是有关它的。 尽管您不必处理数据包丢失问题,但您必须处理更长的运输时间。 TCP比UDP慢,你必须在实时多人游戏中缓解这个问题。 我认为这些原则仍然适用于基本的层面。 我甚至可以使用UDP和Node.js在服务器和Web浏览器之间进行通信,反之亦然? 一般来说,还没有。 至少不是没有某种形式的浏览器扩展或插件 - WebSockets使用TCP。 也就是说,WebRTC(更具体地 ...
  • 如果你想要做的就是开启一个轻量级的HTTP服务器,同时使用C#和.Net编程,你应该给Kayak一个机会。 它是C#的轻量级HTTP服务器,在这种意义上表现为类似于node.js。 kayakhttp 更新: 如果您正在寻找一个轻量级HTTP Server来处理Web请求,那么今天有几个替代方案: ServiceStack(推荐) Microsoft WebAPI NancyFx 据我所知,所有上述工作都在某些版本的Mono上工作,因此您仍然可以在Windows和基于Unix的系统上托管它们。 If all ...
  • 要详细说明driushkin的答案,您应该使用远程过程调用(RPC)和事件队列。 这类似于您发布的图像,其中每个数据包代表一个“命令”或带有一些参数的RPC(即移动方向)。 您还需要一个事件队列来确保按顺序和按时执行RPC。 这将需要时间戳或帧计数来执行每个命令(在未来的某个时间点,以简单的方案)和同步手表(第二次世界大战风格)。 您可能会注意到此方案中的一个关键弱点:由于网络延迟,恶意用户等原因,RPC消息可能会延迟(应在应用之后到达)。在一个简单的方案中,会丢弃延迟的RPC。 这很好,因为所有客户端(甚 ...
  • 您将用于部署和运行您的应用程序的服务器只是远程位置处的另一台计算机(带有操作系统) - 因此是远程服务器! 现在来谈谈你的问题 - 你需要在服务器上连接,安装和运行node.js是一个SSH客户端,例如Putty ( http://www.putty.org/ )。 您可以在Windows机器上下载并运行putty,然后输入您的服务器的主机名/ IP地址。 连接后,您将能够看到服务器的终端窗口。 现在你可以继续安装node.js了。 根据您的服务器机器运行的操作系统,您可以相应地安装node.js。 另外, ...
  • 我同意关于转向基于事件的系统的评论,并且会放弃循环。 我把一个基于文本的处理的快速示例放在一起,可以用于简单的文本游戏。 var fs = require('fs'), es = require('event-stream'); process.stdin .pipe(es.split()) .on('data', parseCommand); var actionHandlers = {}; function parseCommand(command) { var w ...
  • 您可以使用带有Nodejs的Socket.io来获取来自所有客户端的实时数据。 您的应用将连接到将在某处托管的服务器应用。 如果您在localhost(在您的计算机或桌面上)上运行服务器代码,则需要使用ngrok服务来公开它。 您的应用会将玩家坐标发送到服务器,服务器会将这些坐标广播给所有客户/玩家,并为所有玩家播放相同 如果您还需要任何帮助,请告诉我。 在您的客户端应用程序中,在html页面加载此脚本,不完全相似,它将在您的应用程序和服务器应用程序之间创建套接字连接(双工连接)。 然后,您将使用socke ...
  • 您已经在HTML中硬编码了服务器地址: 从中删除服务器地址,就像您对其他脚本一样。 要么: 或者这可行: 取决于脚本在服务器上的物理位置。 我认为第二个可能是你想要的,但我不确定 ...
  • 你需要一个路由器。 const router = express.Router; router.get('/', function(req, res) { res.sendFile(__dirname + "/client"); }); app.use('/', router); 中间件无法处理各种http方法。 You will need a router for that. const router = express.Router; router.get('/', function(req, ...
  • 不是真的需要。 由于节点是基于事件的,并且单个进程将能够处理数千个这样的玩家对。 假设您使用now.js ,您将为每个这样的对创建“房间”/“组”/“通道”。 命名法可能会根据您使用的库而有所不同,但总体方法是相同的 - 将所有将要在相同“频道”中互相对战的玩家分配。 如果你实际上为每个这样的玩家对使用子进程,那么你实际上是在扼杀构建node.js的目的。 另一方面,在forever.js (以及cluster.js )中实现了生成子进程的现实世界用例。 这里有一个子进程(也称为worker)由主进程生成。 ...
  • 如果任何一个单独的游戏进程可能吃掉所有的内存或CPU,这是不可扩展的。 如果你的服务器是8核机器,那么八个游戏可以占用所有的CPU时间,除了通过top监视进程并根据需要杀死它们之外,没有什么可以做的,但这会造成一个颠簸的服务器。 现在,如果你设法首先防止这些东西(对我来说听起来更好),它是可行的。 每个进程将占用30mb以上的内存,因此您需要为每几百人增加一台强大的服务器。 以http://site.nodester.com为例,它们似乎在一台机器上运行大约600个进程。 他们的软件堆栈也是开源的: htt ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)